I am getting this error
flutter: response body:{"Message":"The request entity's media type 'text/plain' is not supported for this resource."}
which i am getting in post whenever i am changing the body type to text instead of json. How can i solve this.
here are the postman screenshots of both successful request and field request are given below
Here is the post method
Future<String> sendRequest() async {
var jsonArray = json.encode(body);
algo.Encrypted encrypted = Algorithm.encrypt(algorithmKey, jsonArray);
var encryptedBosy = jsonEncode({'body': encrypted.base64});
var response = await https.post(
Uri.parse("$baseUrl/$paymentMethod"),
headers: {
"Key": key,
"secretKey": secretKey,
},
body: encryptedBosy,
);
return response.body;
}
Add the content type header to the request.
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
Related
I am trying to push sheets data from a quiz to an api. The put works fine in postman but I can't seem to get it to work in google scripts.
Postman
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
myHeaders.append("Authorization", "Basic bmljazpuaWNr");
myHeaders.append("Cookie", "FB_ResponseHeaders=%7b%22X-XSS-Protection%22%3a%7b%22Key%22%3a%221%3b+mode%3dblock%22%2c%22Value%22%3a%225C-58-58-82-DB-E9-4D-AD-BF-23-F8-5A-65-1E-EE-7B-96-94-7C-E6%22%7d%2c%22X-Content-Type-Options%22%3a%7b%22Key%22%3a%22nosniff%22%2c%22Value%22%3a%22E9-85-E8-2B-38-74-3B-AA-71-5A-41-1D-65-49-06-A7-FF-B3-D2-57%22%7d%2c%22Strict-Transport-Security%22%3a%7b%22Key%22%3a%22max-age%3d31536000%3b+includeSubDomains%22%2c%22Value%22%3a%221D-31-04-88-80-8A-6A-27-8E-2D-72-C5-32-98-74-A9-F8-E8-0C-E7%22%7d%7d; fbd_cooked=!/muNNuj9SlbMhC0PporV/qqu1cgGGvczJaMywTIzq+9GDRYedxCjdCU9X3WnSt1ijLXqndrZJ1DCTVw=");
var raw = JSON.stringify({
"fieldName": "SingleLineText5",
"fieldValue": "question1test"
});
var requestOptions = {
method: 'PUT',
headers: myHeaders,
body: raw,
redirect: 'follow'
};
Google Scripts
// put em in a payload
var payload = {
"fieldName": "SingleLineText7",
"fieldValue": "question1googleSheet" }
var encodedAuth = Utilities.base64Encode("nick:nick");
var headers = [{"Authorization" : "Basic " + encodedAuth},{"Content-Type":"application/json"}];
var options = {
'method': 'PUT',
'body': JSON.stringify(payload),
'headers': headers,
'redirect': 'follow'
}
response = UrlFetchApp.fetch(url, options);
//log the response
Logger.log(JSON.stringify(response));
Am receiving
Exception: Attribute provided with invalid value: headers
If i remove the "Content-Type" I get no error but send no data just returns {}
According to the documentation you are using the wrong parameter. It should be:
const options = {
method : "PUT", // this was correct
payload: JSON.stringify(payload), // correct this
followRedirects : true, // correct this
headers: headers
}
Getting the below issue when I pass the value in variable in json.encode not getting the excepted response, but I when pass the value without variable getting proper response I tried using map and different headers not able to get the exact issue.
Not working
String getvalue = “response-value”;
var _body = json.encode({"context": getvalue});
var res = await http.post(link, headers: {
"Content-Type": "application/json",
}, body: _body
);
working
var _body = json.encode({
"responseValue”:
"response-value"
});
var res = await http.post(link, headers: {
"Content-Type": "application/json",
}, body: _body
);
It is solved, there was issue an issue in the backend api which got solved know.
I'm trying to receive an access token from the Zoom api via Oauth. No matter what form I try and send the body as, 'Content-Type': 'application/json' or Content-Type:application/x-www-form-urlencoded, it always errors to { reason: 'Missing grant type', error: 'invalid_request' }.
var options = {
method: "POST",
url: "https://zoom.us/oauth/token",
body: JSON.stringify({
grant_type: "authorization_code",
code: process.env.AUTH_CODE,
}),
redirect_uri: "https://zoom.us",
};
var header = {
headers: {
Authorization:
"Basic " +
Buffer.from(process.env.ID + ":" + process.env.SECRET).toString("base64"),
},
"Content-Type": "application/json",
};
var tokCall = () =>
axios
.post("https://zoom.us/oauth/token", options, header)
.then((response) => {
console.log(response);
})
.catch((error) => {
console.log(error.response);
});
tokCall();
I'm fairly certain the answer lies in either the data type in which Oauth is receiving the data, or where/if it's receiving the body at all. Any suggestions would be gratefully received.
The error is being thrown because you're sending the data as the body of the post request when the Request Access Token Zoom API is expecting to find them as query parameters which you might know as query strings.
Reference
https://marketplace.zoom.us/docs/guides/auth/oauth#local-test
Image of page from link to highlight the use of query parameters and content-type requirement for API call
Change
var options = {
method: "POST",
url: "https://zoom.us/oauth/token",
body: JSON.stringify({
grant_type: "authorization_code",
code: process.env.AUTH_CODE,
}),
redirect_uri: "https://zoom.us",
};
to
var options = {
method: "POST",
url: "https://zoom.us/oauth/token",
params: {
grant_type: "authorization_code",
code: process.env.AUTH_CODE,
redirect_uri: "<must match redirect uri used during the app setup on zoom>"
},
};
The Content-Type header should be set to application/x-www-form-urlencoded as this is a requirement of the zoom API itself.
BTW, axios requires you to name the body field/object of your request as data and also there's no need for JSON.stringify() method since axios does that for you under-the-hood
Though it's a late answer, I'd like to share it since it took me some time to complete this using Axios.
So to make Zoom authorization, you need to do:
Base64 encode the secret and client id
const base64EncodedBody =
Buffer.from(`${ZOOM_CLIENT_ID}:${ZOOM_CLIENT_SECRET}`).toString('base64');
URI encode the grant_type, code and redirect_uri
const data =
encodeURI(`grant_type=authorization_code&code=${code}&redirect_uri=${redirectUri}`);
Send the request
const response = await axios.post('https://zoom.us/oauth/token', data, {
headers: {
Authorization: `Basic ${base64EncodedBody}`,
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(data),
},
});
I'm trying to pass a Json body inside a Json body using flutter.
The function is as follow:
Future<String> postItem(Item item, Brand brand) async {
var jsonob = jsonEncode(<String, dynamic>{
'item_code': item.itemCode,
'item_name': item.itemName,
'part_no': item.partNo,
'min_stock': int.parse(item.minStock),
'brand': jsonEncode(brand)
});
print(jsonob); //for debugging
final http.Response response = await http.post(
BASE_URL + "item",
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonob
);
Brand the following function to encode to json:
Map<String, dynamic> toJson() => {
"id": int.parse(this.brandId),
"brand_name": this.brandName,
"brand_code": this.brandCode,
"brand_image": this.brandImage
};
error message:
E/flutter ( 1601): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Exception: {error: entity validation failed, reasons: [invalid input type for 'brand']}
output of print for jsonob:
{"item_code":"item-001","item_name":"item123","part_no":"part123","min_stock":1,"brand":"{\"id\":1,\"brand_name\":\"BRANDA\",\"brand_code\":\"BRANDA\",\"brand_image\":\"null\"}"}
I'm using the guidelines from here: https://flutter.dev/docs/cookbook/networking/send-data
The http server is created using Aqueduct.
I tried running the same http.post using POSTMAN and there was no error thrown, so I'm unsure what is causing this error in flutter.
Any help would be greatly appreciated.
i think request body should encoded to a Json string using JsonEncode(Object)
so it should be that way :
final http.Response response = await http.post(
BASE_URL + "item",
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(jsonob),
);
I'm trying to send a http request to Githubs graphql api(v4). My guess is that the format of my query is wrong. The code below is used to send a POST request to the api.
app.get('/fetch-data', function(req, res, next) {
const access_token = settings.dev.TOKEN;
const query = {query: { viewer: { 'login': 'muckbuck' } }};
const options = {
uri: 'https://api.github.com/graphql',
method: 'POST',
headers: {
'Accept': 'application/json',
'Authorization': "Bearer " + access_token,
'User-Agent': 'request',
'contentType': "application/graphql",
},
data: query
};
function callback(error, response, body) {
console.log(response.body)
if (!error && response.statusCode == 200) {
console.log(body);
console.log('lol');
}
}
request(options, callback);
});
The error message that I get:
{"message":"Problems parsing JSON","documentation_url":"https://developer.github.com/v3"}
I believe your GraphQL is malformed in that the query key is supposed to contain a string, not a complex structure. See also this example from the GitHub API documentation.
You may also want to set Content-Type to application/json as is recommended by the GraphQL introduction. application/graphql does not seem to be a registered media type and appears to alter the behaviour of GraphQL.