Forum Discussion

David91's avatar
David91
Frequent Contributor
2 years ago

POST request return 400 Bad request

Hi,

I have a problem with a POST request to the server. Thanks to basic auth I managed to authenticate but the request still returns 400 Bad Request. Do you know what I am doing wrong. The same points through the postman application pass to 201 code.

 

Thank you for your advice!

 

My code:

function PostImportProduct()
{
var address = "https://logdemota-dev.st.test.cz:10351/MYSTOCKLOGISTICSTAWMSINT_DEV/V1/product";
var username = "API_TEST";
var password = "xxxxxx";

// Define the request body JSON string
var requestBody = '{"extIsId":"abc/777","productCode":"ExterniKodSort","name":"TA_API777","type":0,"measurementUnitCode":"KS","weightGross":1.1,"weightNett":1.1,"grossDimension":{"height":1.1,"width":1.1,"depth":1.1,"volume":1.1},"pictureUrl":"www.aaa.cz/SORT.png","expirationMandatory":1,"serialNumbersRecords":{"inboundMandatory":1,"outboundMandatory":1},"batches":1,"barcodes":[{"barcode":123456,"active":1,"default":1,"measurementUnitCode":"ks"}]}surementUnitCode":"KS","warehouseCode":"3S20","weightGross":10.5,"weightNett":9.5,"grossDimension":{"height":2,"width":1.5,"depth":3.5,"volume":10.5},"pictureUrl":"","expirationMandatory":1,"serialNumbersRecords":{"inboundMandatory":1,"outboundMandatory":1},"batches":1,"barcodes":[{"barcode":"EAN80","active":1,"default":1,"measurementUnitCode":"KS"},{"barcode":"EAN81","active":1,"default":0,"measurementUnitCode":"KS"}]}';

// Convert the user credentials to base64 for preemptive authentication
var credentials = aqConvert.VarToStr(dotNET.System.Convert.ToBase64String
(dotNET.System_Text.Encoding.UTF8.GetBytes_2(username + ":" + password)));

Log.Message(credentials)

var aqHttpRequest = aqHttp.CreatePostRequest(address, username, password);
// Send the Authorization header with a request
aqHttpRequest.SetHeader("Authorization", "Basic " + credentials);

var aqHttpResponse = aqHttpRequest.Send(requestBody);

// Read the response data
Log.Message(aqHttpResponse.AllHeaders); // All headers

Log.Message(aqHttpResponse.StatusCode); // A status code
Log.Message(aqHttpResponse.StatusText); // A status text
Log.Message(aqHttpResponse.Text); // A response body
}

 

Console log.

 

  • This will be difficult to diagnose, since 400 Bad Request response, indicates that the server cannot or will not process the request due to something that is perceived to be a client error.

     

    Saying that, it looks like your JSON is incorrectly formatted (shown in red),

     

    '{"extIsId":"abc/777","productCode":"ExterniKodSort","name":"TA_API777","type":0,"measurementUnitCode":"KS","weightGross":1.1,"weightNett":1.1,"grossDimension":{"height":1.1,"width":1.1,"depth":1.1,"volume":1.1},"pictureUrl":"www.aaa.cz/SORT.png","expirationMandatory":1,"serialNumbersRecords":{"inboundMandatory":1,"outboundMandatory":1},"batches":1,"barcodes":[{"barcode":123456,"active":1,"default":1,"measurementUnitCode":"ks"}]}surementUnitCode":"KS","warehouseCode":"3S20","weightGross":10.5,"weightNett":9.5,"grossDimension":{"height":2,"width":1.5,"depth":3.5,"volume":10.5},"pictureUrl":"","expirationMandatory":1,"serialNumbersRecords":{"inboundMandatory":1,"outboundMandatory":1},"batches":1,"barcodes":[{"barcode":"EAN80","active":1,"default":1,"measurementUnitCode":"KS"},{"barcode":"EAN81","active":1,"default":0,"measurementUnitCode":"KS"}]}';

     

     

  • David91's avatar
    David91
    2 years ago

    i use JSON.stringify and it works..

3 Replies

  • David91's avatar
    David91
    Frequent Contributor

    I tried to help

    aqHttpRequest.SetHeader("Content-Type", "application/json");

    to set the Content type to JSON, but it keeps returning text/html

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    This will be difficult to diagnose, since 400 Bad Request response, indicates that the server cannot or will not process the request due to something that is perceived to be a client error.

     

    Saying that, it looks like your JSON is incorrectly formatted (shown in red),

     

    '{"extIsId":"abc/777","productCode":"ExterniKodSort","name":"TA_API777","type":0,"measurementUnitCode":"KS","weightGross":1.1,"weightNett":1.1,"grossDimension":{"height":1.1,"width":1.1,"depth":1.1,"volume":1.1},"pictureUrl":"www.aaa.cz/SORT.png","expirationMandatory":1,"serialNumbersRecords":{"inboundMandatory":1,"outboundMandatory":1},"batches":1,"barcodes":[{"barcode":123456,"active":1,"default":1,"measurementUnitCode":"ks"}]}surementUnitCode":"KS","warehouseCode":"3S20","weightGross":10.5,"weightNett":9.5,"grossDimension":{"height":2,"width":1.5,"depth":3.5,"volume":10.5},"pictureUrl":"","expirationMandatory":1,"serialNumbersRecords":{"inboundMandatory":1,"outboundMandatory":1},"batches":1,"barcodes":[{"barcode":"EAN80","active":1,"default":1,"measurementUnitCode":"KS"},{"barcode":"EAN81","active":1,"default":0,"measurementUnitCode":"KS"}]}';

     

     

    • David91's avatar
      David91
      Frequent Contributor

      i use JSON.stringify and it works..