Problem with API JSON api body request

I'm trying to reach an api which require the following format:


Desired input:

{

 "user_id": "0",

 "payment_id": "2",

 "shipping_id": "1",

 "products": {

     "1": {

       "product_id": "12",

       "amount": "1"

     }

 },

 "user_data": {

    "email":"guest@example.com",

    "firstname": "Guest",

    "lastname": "Guest",

    "s_firstname": "Guest",

    "s_lastname": "Guest",

    "s_country": "US",

    "s_city": "Boston",

    "s_state": "MA",

    "s_zipcode": "02125",

    "s_address": "44 Main street",

    "b_firstname": "Guest",

    "b_lastname": "Guest",

    "b_country":"US",

    "b_city": "Boston",

    "b_state": "MA",

    "b_zipcode":"02125",

    "b_address": "44 Main street"

 }

}



Unfortunately, the request is rejected with the following info:

HTTP/1.1 400 BadRequest

Content-Type: application/json

Transfer-Encoding: chunked

Date: Thu, 23 Jan 2020 08:41:43 GMT

Server: nginx

{

  "message": "Bad Request: The user_id/user_data field is required",

  "status": 400

}



This is the current Outsystems body what I insert to the body of the api:

"{

""user_id"":""0"",

""payment_id"":""60"",

""shipping_id"":""11"",

""products"":{""products"":[{""product_id"":""12"",""amount"":""1""}]}

,""user_data""{

""email"":""guest@example.com"",

""firstname"":""Guest"",

""lastname"":""Guest"",

""s_firstname"":""Guest"",

""s_lastname"":""Guest"",

""s_country"":""US"",

""s_city"":""Boston"",

""s_state": ""MA"",

""s_zipcode"":""02125"",

""s_address"":""44 Main street"",

""b_firstname"":""Guest"",

""b_lastname"":""Guest"",

""b_country"":""US"",

""b_city"":""Boston"",

""s_state": ""MA"",

""b_zipcode"":""02125"",

""b_address"":""44 Main street""}

}"


What is wrong in my request? I know there is some difference in the format, but what could it be?

Seems to be something in this sence:

""products"":{""products"":[{""product_id"":""12"",""amount"":""1""}]}


Your help is very welcome. Thank you in advance.

The response suggests user_id is the problem. Have you tried declaring the value without double "? 

Example:   "user_id": 0,


Also should the user_id be part of the User_data collection?

Example:

"User_data" : {

             "User_id":


If User_id is a primary key, it could also be that 0 is invalid.

-Yes we tried but then we get the same error.


-No the desired input is from the documentation so it should not.


-User_id 0 means that this user is a guest user



Hi,

Have you tried to test the API in postman as well, if it works in postman then you can compare the request format for both, sometimes a little change made the huge difference in API call.

Cheers

Well, it seems to be the difference is in here:


My input request:

""products"":{""products"":[{""product_id"":""12"",""amount"":""1""}]}


The needed request that should work:

 "products": {

     "1": {

       "product_id": "12",

       "amount": "1"

     }

 },


The origin api documentation says:
products*—an object (associative array) with the information about the ordered products. There are two ways how you can specify a product:


Does anybody know how to create such an associative array? or how should I remove the [ ] chars in my request?

Your information is very helpful. Thank you in advance. 


Hi DDK,

First of all go through the below link for API implementation:

https://success.outsystems.com/Documentation/11/Extensibility_and_Integration/REST/Consume_REST_APIs/Consume_One_or_More_REST_API_Methods 

when we consume any API in outsystems, it will create the request structure automatically, if you have the API documentation then i hope you have sample request as well? you can test the API on the basis of sample request and then you will get the request format structure in your application.

Hope this helps.

Cheers

Hi, 

Yes, I read it already. My main problem is the encoding type, I guess.

How can I convert this input


"{

""user_id"":""0"",

""payment_id"":""60"",

""shipping_id"":""11"",

""products"":{""products"":[{""product_id"":""12"",""amount"":""1""}]},


To this output I JSON en Outsystems?
 

{

 "user_id": "0",

 "payment_id": "2",

 "shipping_id": "1",

 "products": {

     "1": {

       "product_id": "12",

       "amount": "1"

     }

 },


Thank you for your help.