This is my code to make a request to an API:
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;
Future<http.Response> postRequest () async {
var url ='https://pae.ipportalegre.pt/testes2/wsjson/api/app/ws-authenticate';
var body = jsonEncode({ 'data': { 'apikey': '12345678901234567890' } });
print("Body: " + body);
http.post(url,
headers: {"Content-Type": "application/json"},
body: body
).then((http.Response response) {
print("Response status: ${response.statusCode}");
print("Response body: ${response.contentLength}");
print(response.headers);
print(response.request);
});
}
I have a problem with the response from the request, where its suppose to have a body with json, but something went wrong and i think is with the json that i send on the body request, because it is a nested json object, and the value of the key is a json object. i would love to know how i can parse the json right and insert into body of the request.
this is the header response:
{set-cookie: JSESSIONID=DA65FBCBA2796D173F8C8D78AD87F9AD;path=/testes2/;HttpOnly, last-modified: Thu, 10 May 2018 17:15:13 GMT, cache-control: no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0, date: Thu, 10 May 2018 17:15:13 GMT, content-length: 0, pragma: no-cache, content-type: text/html, server: Apache-Coyote/1.1, expires: Tue, 03 Jul 2001 06:00:00 GMT}
and this is how is suppose to be:
Server: Apache-Coyote/1.1
Expires: Tue, 03 Jul 2001 06:00:00 GMT
Last-Modified: Thu, 10 May 2018 17:17:07 GMT
Cache-Control: no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0
Pragma: no-cache
Content-Type: application/json;charset=UTF-8
Vary: Accept-Encoding
Set-Cookie: JSESSIONID=84813CC68E0E8EA6021CB0B4C2F245BC;path=/testes2/;HttpOnly
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
the body response came empty and i think its because the body i sent on the request, can anyone help me with the nested json object in value??
SCREENSHOT OF POSTMAN:
url
anduri
? – Mckinneyjson.encode(...)
you send a string not JSON. Just send{ 'data': { 'xpto': '12345678901234567890' } }
directly if you want it to be treated as JSON. – AssisitoString
of the Dart map. If you want to see each item, print them like this:response.headers.forEach((a, b) => print('$a: $b'));
. If you pass thehttp
package aMap
it expects it to be aMap<String, String>
containing POST form entities, so you can't send json that way (unless you encode it yourself first). Can you update the question showing either some documentation of the web API or sample code in another language? – Bodyworkhttp
would have made it more clear that your are using this package. – AssisiAccept: application/json
in addition to'content-type'
. If this doesn't help it's unlikely to be related to Dart or the client code, but rather an issue with the server. – Assisi