FusionAuth
    • Home
    • Categories
    • Recent
    • Popular
    • Pricing
    • Contact us
    • Docs
    • Login

    Invalid JSON error when trying to retrieve users using user API

    Scheduled Pinned Locked Moved Solved
    Q&A
    erro json user-api
    2
    3
    3.1k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      munkith.abid
      last edited by

      Hi, I'm currently working on a task to migrate my corporate users from current AnA service to fusionauth.
      Part of the initial stages is writing a script (I decided to go with node js) to check if any of the users to be migrated already exist in fusionauth but I ran into the issue detailed below. It's worth mentioning that I can't use any node library other than the core once for technical reasons, this includes not installing fusionauth API.
      Node code snippet where the error raised:

      const apiToken = '<REQUIRED_API_TOKEN>';
      
      function checkUserByEmail() {
        const options = {
          hostname: '<OUR_DOMAIN>.fusionauth.io',
          path: '/api/user/<USER_EMAIL>',
          method: 'GET',
          headers: {
            "content-type": "application/json",
            Authorization: apiToken,
          }
        }
        const req = https.request(options, (res) => {
          res.setEncoding('utf8');
          process.stdout.write(`STATUS: ${res.statusCode}\n`);
          process.stdout.write(`HEADERS: ${JSON.stringify(res.headers)}\n`);
          res.on('data', (chunk) => {
            process.stdout.write(`BODY: ${chunk}`);
          });
          res.on('end', () => {
            process.stdout.write('Response ended!');
          });
        });
        req.on('error', e => process.stdout.write(`${e.name}: ${e.message}`));
        req.end();
       .
       .
       .
      }
      

      The error I'm getting is:

      STATUS: 400
      HEADERS: {"date":"Tue, 12 Jul 2022 18:31:00 GMT","content-type":"application/json;charset=UTF-8","content-length":"310","connection":"close","cache-control":"no-store"}
      BODY: {"fieldErrors":{"":[{"code":"[invalidJSON]","message":"Invalid JSON in the request body. The property was []. The error was [Possible conversion error]. The detailed exception was [No content to map due to end-of-input\n at [Source: (org.apache.catalina.connector.CoyoteInputStream); line: 1, column: 0]]."}]}}
      Response ended!
      

      Also, using curl would return the desired result:

         'https://s5stratos-dev.fusionauth.io/api/user?email=<USER_EMAIL>'
      

      will return:

      {"user":{"active":true,"connectorId":"e3306678-a53a-4964-9040-1c96f36dda72","email":"USER_EMAIL","encryptionScheme":"salted-pbkdf2-hmac-sha256","id":"USER_ID","insertInstant":1657571797768,"lastLoginInstant":1657571797768,"lastUpdateInstant":1657571797768,"passwordChangeRequired":false,"passwordLastUpdateInstant":1657571797823,"tenantId":"SOME_ID","twoFactor":{},"usernameStatus":"ACTIVE","verified":true}}
      

      I tried some other variations of the code above with no luck.
      I hope I posted every thing needed and thanks in advance.

      danD 1 Reply Last reply Reply Quote 1
      • danD
        dan @munkith.abid
        last edited by

        @munkith-abid I've run into this a few times. It is because you are setting the Content-Type to application/json yet making a GET request.

        When making a GET request, don't set the Content-type header.

        Try that and let us know what happens.

        --
        FusionAuth - Auth for devs, built by devs.
        https://fusionauth.io

        M 1 Reply Last reply Reply Quote 1
        • M
          munkith.abid @dan
          last edited by

          @dan Oops! thank you so much Dan, it worked as a charm. I was fooled into leaving this header in because I tried the exact same request setup using one of these free api endpoints instead of fusionauth api and it worked with the content-type included. Thanks again

          1 Reply Last reply Reply Quote 1
          • M munkith.abid has marked this topic as solved on
          • First post
            Last post