0

I'm trying to send a JSON array as parameter using HTTP native with IONIC 3. This is the request:

 HTTPResponse = await this.http.post(url, 
 body, headers);

This is the complete code. I'm using HTTP from from '@ionic-native/http'. This is the complete code

 import { HTTP, HTTPResponse } from '@ionic-native/http';

 [...]

 let body =  '[{"userId": 1, "timestamp":"2018-10-12T18:00:00.000+02", 
 "audit":"MENUTEST"},{"userId": 1, "timestamp":"2018-10-
 12T18:00:00.000+02", "audit":"MENUTEST"},{"userId": 1, 
 "timestamp":"2018-10-12T18:00:00.000+02", "audit":"MENUTEST"}]';

 //Auth header
 let headers = { Authorization: `Bearer ${token}`};

 let httpResponse: HTTPResponse = await 
 this.http.post(URL_data, JSON.parse(body), headers); 

I'm using similar code with other request and all is going fine, the only difference is that the request that is going have a simple parameters in JSON body. This is the request that is going fine:

import { HTTP, HTTPResponse } from '@ionic-native/http';

[...]

let body = '{ "username": "usuario.prueba1", "password": "' + 
Md5.hashStr('prueba') + '", "customerCode": "1234DEV" }';

let httpResponse: HTTPResponse = await 
this.http.post(URL_login, JSON.parse(body), headers); 

2 Answers 2

0

Updated answer:

If you have a look at the source-file of the android implementation of the post action you will see that a JsonObject is expected as parameter, this is why passing an array will result in a JSON error. You have to wrap the array in an object like that to make this work:

let body = {
  myArray: [
    {
      userId: 1,
      timestamp: '2018-10-12T18:00:00.000+02',
      audit: 'MENUTEST',
    },
    {
      userId: 1,
      timestamp: '2018-10-12T18:00:00.000+02',
      audit: 'MENUTEST',
    },
    {
      userId: 1,
      timestamp: '2018-10-12T18:00:00.000+02',
      audit: 'MENUTEST',
    },
  ],
};

let headers = { Authorization: `Bearer ${token}` };
let httpResponse: HTTPResponse = await this.http.post(
  URL_data,
  body,
  headers,
);

Old answer:

I guess your Content-Type is application/x-www-form-urlencoded so you need to add a name for you parameter which is separated from the value by an equal sign (=):

let payload = [{ "userId": 3, "timestamp": "2018-10-12T18:00:00.000+02", "audit": "Mensaje 3" }, { "userId": 4, "timestamp": "2018-10-13T18:00:00.000+02", "audit": "Mensaje 4" }];
let body = `MyArray=${payload}`;

The relevant part of the specification can be found here. Point 2. says:

The control names/values are listed in the order they appear in the document. The name is separated from the value by `=' and name/value pairs are separated from each other by `&'.

Sign up to request clarification or add additional context in comments.

7 Comments

I have edited the question in order to give you more information. The issue is related to IONIC 3 and i need a valid code for this framework.
Ah you should have mentioned that you are using the native HTTP plugin, please have a look at the updated answer.
I have tested your code but it's not valid for server side. I have tried to send JSON by REST client and all is going fine. but i cannot create the same body in my client app. Your code include myarray field that it is not expected by server. Any addional idea?
Then you have to change either your server side code so it can read json object or you do not use the ionic-native http plugin. You could also use angulars HttpModule to make this call.
FInally i have used angulars module, but the main problem i had was -c -l paramaters in app execution. These parameters are modifying behaviour in request. Content-type was being ignored. Thanks a lot, but the problem probably was not in the code.
|
-1

To support deep structure, you should change serializer. Try set http.setDataSerializer("json"); And send data as usual: http.post(url, body, {})

Check this answer:
https://stackoverflow.com/a/49589124/6097876

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.