JSONError: No data, empty input at 1:1

I would like to know why I am getting this error: “There was an error in evaluating the Pre-request Script: Error: Unexpected token ‘<’ at 1:1 ^”. Despite that, the Pre request seemed to run perfectly and I got a successful response in the console. For the actual request, I got this on the console: “JSONError: No data, empty input at 1:1^”.

What and where could I have been wrong?

1 Like

Hi @calebonigbinde,

It seems like you’re likely have some html or XML somewhere, and your pre-request cannot understand it, as it is in there now.

What would be most beneficial is if you can share a screenshot for us, and we can more easily troubleshoot the issue.

Best,
Orest

1 Like
var kuwego_cba = pm.environment.get("kuwego-cba");
var scheme_id = pm.environment.get("scheme_id");
var scheme_send_url = kuwego_cba + "/api/v1/ledger-account-schemes/" + scheme_id;
var customer_send_url = kuwego_cba + "/api/v1/customers";
var account_send_url = kuwego_cba + "/api/v1/ledger-accounts";
var token = pm.environment.get("kuwego_admin_token");
var account_number_length = pm.environment.get("scheme_account_length");
var prefix = pm.environment.get("scheme_account_prefix");


const scheme_payload = {
    url: scheme_send_url,
    method: 'PUT',
    header: {
        'Authorization': 'bearer ' + token,
        'Content-Type': 'application/json'
        },
    body: {
        mode: 'raw',
        raw: JSON.stringify({
    code: 'anything',
	name: pm.environment.get("scheme_name"),
	status: 'ACTIVE',
	classification: "INCOME",
	allowNegativeBalance: true,
	allowLien: true,
    accountNumberPrefix: prefix,
    accountNumberLength: account_number_length,
	enableInstantBalanceUpdate: true,
    schemeRequirement: {
		signature: true,
		residentialAddress: true,
		employment: true,
		nextOfKin: true,
		identification: true,
		spouse: true
	}
        })
    }

};

pm.sendRequest(scheme_payload, function (err, res) {
    // jsonData = res.json();
    // if (err) {
    //     console.log(err);
    // } else {
    //     // account_number_length = jsonData.data.accountNumberLength;
    //     // pm.environment.set("scheme_id", jsonData.data.id);
    //     // pm.environment.set("scheme_account_length", account_number_length);

    // }
   
});


var random_gen = pm.variables.replaceIn('{{$randomBankAccount}}{{$timestamp}}');
console.log (random_gen);
var request_length = parseInt(account_number_length) - prefix.length ;
console.log (request_length);
var refined_gen = prefix + random_gen.substring(0, request_length);
console.log (refined_gen);

pm.environment.set("generated_account_no", refined_gen);


const customer_payload = {
    url: customer_send_url,
    method: 'POST',
    header: {
        'Authorization': 'bearer ' + token,
        'Content-Type': 'application/json'
        },
    body: {
        mode: 'raw',
        raw: JSON.stringify({
    firstName: pm.variables.replaceIn('{{$randomLastName}}'),
	lastName: pm.variables.replaceIn('{{$randomLastName}}'),
	gender: "FEMALE",
	dateOfBirth: "2020-08-03",
	phoneNumber: "07033031209",
	emailAddress: "conigbinde"+pm.variables.replaceIn('{{$randomPassword}}')+"@teamapt.com",
	passportRef: pm.variables.replaceIn('{{$randomUUID}}'),
    bankVerificationNumber: "08" + pm.variables.replaceIn("{{$randomBankAccount}}") + "1",
    maritalStatus: "SINGLE",
    status: null,
    countryCode: null,
    nationality: "NG",
    title: pm.variables.replaceIn('{{$randomNamePrefix}}'),
    middleName: pm.variables.replaceIn('{{$randomFirstName}}'),
    signatureRef: pm.variables.replaceIn('{{$randomUUID}}'),
    placeOfBirth: "ydzzqmldafdixethb",
    stateOfOrigin: "Lagos",
    localGovernmentOfOrigin: "Eti-Osa",
    motherMaidenName: pm.variables.replaceIn('{{$randomLastName}}'),
    religion: "Christianity",
    taxIdentificationNumber: pm.variables.replaceIn('{{$timestamp}}'),
    residentialAddress: {
        addressLineOne: "No" + pm.variables.replaceIn("{{$randomInt}}{{$randomAlphaNumeric}}"),
        addressLineTwo: pm.variables.replaceIn("{{$randomStreetName}}"),
        city: "Lagos",
        localGovernment: "Eti-Osa",
        state: "Lagos",
        countryCode: "NG",
        verificationRef: pm.variables.replaceIn("{{$randomUUID}}")
    },
    mailingAddress: {
        addressLineOne: pm.variables.replaceIn("{{$randomStreetAddress}}"),
        addressLineTwo: pm.variables.replaceIn("{{$randomStreetName}}"),
        city: pm.variables.replaceIn("{{$randomCity}}"),
        localGovernment: "Agege",
        state: "Lagos",
        countryCode: "NG",
        verificationRef: pm.variables.replaceIn("{{$guid}}")
    },
    employmentInfo: {
        employerName: pm.variables.replaceIn("{{$randomFullName}}"),
        natureOfBusiness: pm.variables.replaceIn("{{$randomProductName}}"),
        annualIncome: 4.00,
        employmentDate: "2020-08-03",
        employmentStatus: "CURRENT",
        officePhoneNumber: "07033031209",
        officeFaxNumber: "07033031209",
        employmentLetterRef: pm.variables.replaceIn("{{$randomUUID}}"),
        employmentIdRef: pm.variables.replaceIn("{{$randomUUID}}")
    },
    nextOfKin: {
        addressLineOne: pm.variables.replaceIn("{{$randomStreetAddress}}"),
        addressLineTwo: pm.variables.replaceIn("{{$randomStreetName}}"),
        city: pm.variables.replaceIn("{{$randomCity}}"),
        localGovernment: "Ikorodu",
        state: "Lagos",
        countryCode: "NG",
        verificationRef: null,
        title: pm.variables.replaceIn("{{$randomNamePrefix}}"),
        fullName: pm.variables.replaceIn("{{$randomFullName}}"),
        dateOfBirth: "2020-08-03",
        gender: "MALE",
        relationship: "Sibling",
        phoneNumber: "08030000007",
        emailAddress: "user8@teamapt.com"
    },
    identificationInfo: {
        identificationType: "International Passport",
        registrationNumber: pm.variables.replaceIn("{{$randomBankAccount}}"),
        issueDate: "2020-08-03",
        expiryDate: "2022-08-03",
        scannedImageRef: pm.variables.replaceIn("{{$randomUUID}}")
    },
    spousalInfo: {
        fullName: pm.variables.replaceIn("{{$randomFullName}}"),
        dateOfBirth: "2020-08-03",
        occupation: "rooulbazebobzsvtgnjhzmegbrw"
    },
    location: {
        latitude: pm.variables.replaceIn("{{$randomLatitude}}"),
        longitude: pm.variables.replaceIn("{{$randomLongitude}}")
    }
    })
    }
};

pm.sendRequest(customer_payload, function (err, res) {
    jsonData = res.json();
    if (err) {
        console.log(err);
    } else {
       var customer_code = jsonData.data.code;
        pm.environment.set("pair_customer_code", customer_code);
        // pm.environment.set("scheme_account_length", account_number_length);

    }
   
});

const account_payload = {
    url: account_send_url,
    method: 'POST',
    header: {
        'Authorization': 'bearer ' + token,
        'Content-Type': 'application/json'
        },
    body: {
        mode: 'raw',
        raw: JSON.stringify({
	schemeCode: pm.environment.get("scheme_code"),
	accountCode: pm.variables.replaceIn("{{$randomBankAccount}}{{$randomAlphaNumeric}}"),
	accountNumber: refined_gen,
	accountName: "Aptpay" + pm.variables.replaceIn("{{$randomBankAccountName}}"),
	parentAccountId:"",
	hierarchyNumber:"0",
	currencyCode:"566",
	allowCredit: true,
	allowDebit: true,
	allowOwnEntry: true,
	status:"active"
    })
    }
};

pm.sendRequest(account_payload, function (err, res) {
    jsonData = res.json();
    if (err) {
        console.log(err);
    } else {
        var account_number = jsonData.data.accountNumber;
        pm.environment.set("pair_account_number", account_number);
        // pm.environment.set("scheme_account_length", account_number_length);

    }
   
});

Here is the snippet of the code in the pre-request

Thanks for providing the script @calebonigbinde.

From what I see, chances are its related to you getting back an html or xml response, and your res.json() cannot parse it properly.

Since you have quite the large script, its going to be hard to find out which request it is thats having issues, but you can do them individually to see which one is failing.

Would you happen to know if the error gives a line number associated with the error?

1 Like

Alright. I can do this: I will isolate parts of the scripts and get which part exactly gives this error. Hopefully, the problem has to do with that unit itself and not its integration with the other part of the script.
The error never specifies the line number associated with it.

Yea sadly sometimes it takes a lot of tinkering to break it down and see where the problem lies. Hopefully it’s not at an integration point. Let me know if you find it and if you have any more questions.

Best,
Orest

1 Like

I’m getting the exact same error for a very simple request/response.
Anyone got more ideas why?

This is my request (no body): {{url}}/api/v1/spring/job/delete/bogHotel/10000

This is the response, status 200:
{
“number”: 0,
“success”: true,
“message”: null,
“result”: null
}
This is my Test Code:
pm.test("Delete job " + job, function () {
pm.expect(pm.response.code).to.be.oneOf([200, 201, 202]);
});

This is the error in the console: JSONError: No data, empty input at 1:1

(I’m using Postman version v7.36.0)

This error generally happened when you are trying to get Json response but there is no Json response body. e.g. if there is no response in the response body but you are still trying to execute below " var jsonData = pm.response.json()" , you will get json error.

4 Likes

Hi @tkloeber,

Yes as @svarshney2311 said when the response type is not JSON and you are trying to parse using JSON function it will thrown you an error. But as per your statement, to check the Status code, it shouldn’t throw any error. Also you can check your response type here:

1 Like

Hi Priscilla and Varshney,

thank you for your reply.
Alas, as far as I can see, none of your suggestions apply in my case.

  1. I’m not trying to retrieve any JSON from the response
  2. the response content-type is marked as application/json
  3. the response body is shown as JSON, so there is JSON data available
    (see the attached screenshot)

any other suggestions what I might be doing wrong or is missing.
Thanx in advance for your help!
Thomas

1 Like

I know this isn’t answering your question directly, but you’re going to get an error with your test scripts because it’s using an undefined variable in line 1.

I don’t see where you are defining job to use in the test name.

1 Like

Thanx, Allen! It didn’t quite answer the question but it solved the issue :smiley:

The job-variable was just a left-over from a more elaborate test script which I reduced for readability.

So, if I reference an undefined variable I get this JSONError, odd :thinking:

2 Likes

Thank you @svarshney2311 and @bpricilla This was exactly my problem and I solved it. It couldn’t parse the empty error response. It was actually a “401Unauthorized” error.

1 Like

This was a perfect explanation to this Error. Earlier my tests were unable to run because of this error as “var jsonData = pm.response.json()” was the very first line in my tests section. As soon as I added a response body to my sample request, my tests ran as expected.

I think that I have a solution for that…
Check the console
Maybe in the Request headers is returning this:
Request headers :
Content-Type: text/plain

If it is, in your pre request script, where you assign the all the request, you will have to specify the headers like this example

const getAuthorizationtoken = {
url: AmbienteQC + url_epLogin2,
method: ‘POST’,
header: {‘Content-Type’: ‘application/json’}, //add this line.
body: {
mode: ‘raw’,
raw: JSON.stringify({

This is when one of you global, envirinment, or collection variables is trying to be retrieved. specifically an object stored as a string. You will see tis when trying to JSON.parse an empty / null or undefined variable

check on how you render json responses: eg install jbuilder/ serializer (for ruby/Rails)