I have an API that expects a JWT token.
I tested the API with https://hoppscotch.io, here is the query that returns the expected results (no error)
import axios from "axios";
const options = {
method: 'POST',
url: 'https://my_authority/text-message',
headers: {
Authorization: 'Bearer my_token',
'content-type': 'application/json'
},
data: {message: ''}
};
axios.request(options).then(function (response) {
console.log(response.data);
}).catch(function (error) {
console.error(error);
});
And here is the code in Dart that fires the error ClientException (Failed to parse header value):
await http.post(
Uri.parse('$_httpEndpoint/$path'),
headers: {
HttpHeaders.contentTypeHeader: 'application/json',
HttpHeaders.authorizationHeader: 'Bearer my_token',
},
body: jsonEncode(body));
Both queries have the same parameters. I have no clue about the root causes.
Here is requests.headers in locals for IOClient.send when the error fires:
_CompactLinkedCustomHashMap ({content-type: application/json; charset=utf-8, authorization: Bearer my_token})
I don't know why http adds charset=utf-8, and I didn't manage to remove it.
I looked over the internet and didn't find an answer. Most of the similar questions are related to an error in the backend, it is not the case here.
Related
I am using Dio library and everything is ok with Get and Post but when I use Delete it shows the 405 error from application only.
When I try it on swagger it works fine?
The head of BaseOptions for entire application requests:
headers: {
'Accept': 'application/json'
'Content-Type': 'application/json'
}
The request is:
dio.delete(path, data: json.encode(data));
The response should be:
{success: true, data: true}
My workflow's script with action/github-script(v6) step:
const response = await github.request('POST https://example.com', {
headers: {
authorization: 'Bearer xxx',
accept: 'application/vnd.heroku+json; version=3', // I want header to be like this
'content-type': 'application/json'
},
// some other options, like request body...
});
console.log(response);
When the accept and other HTTP headers are automatically overriden with:
{
status: 400,
reponse: {}, // not important, body complains about incorrect Accept header
request: {
method: 'POST',
url: 'example.com',
headers: {
accept: 'application/vnd.github.-preview+json', // wtf?
authorization: 'token [REDACTED]', // wtf? it should start with "Bearer"
'content-type': 'application/json', // ok, as expected
'user-agent': 'actions/github-script octokit-core.js/3.5.1 Node.js/16.13.0 (linux; x64)' // ok, but I didn't set this...
},
// other stuff...
}
Now the question is what am I missing? Can I make truthly custom request using github.request() api like that?
I'm having trouble setting the Content-Type header in axios.
Here's my code:
axios({
url: fetchUrl,
data: JSON.stringify(fetchOptions.body),
method: 'POST',
headers: {
'Content-Type': 'application/vnd.api+json',
Accept: 'application/vnd.api+json',
},
})
And here's the request headers:
Accept is set correctly but Content-Type is not. (Confirmed by removing Accept in my code, in which case the request header reverts to json isntead of vnd.api+json.)
When I change Content-Type to ContentType, then I see ContentType in the Response headers, so the problem is specifically with Content-Type.
It turns out that this error was the result of having an empty data; the property was called data, but I mistakenly called body:
axios({
url: fetchUrl,
data: JSON.stringify(fetchOptions.data),
method: 'POST',
headers: {
'Content-Type': 'application/vnd.api+json',
Accept: 'application/vnd.api+json',
},
})
Im trying to make a http-request to Filemaker with Flutter(package:http/http.dart)
I can get the token normally, but if i try the make an _find request to Filemaker it always get's rejected(400 Bad Request) without any message.
In Postman I can do the exact same request without issue!
var body = { "query":[{
"loginName": "==testUser#test.com"
}]};
Response response = await post(url,
headers: {
HttpHeaders.authorizationHeader: 'Bearer $token',
HttpHeaders.contentTypeHeader: 'application/json'},
body: json.encode(body));
Found it:
Dart http adds: content-type: application/json; charset=utf-8
And Filemaker rejects this..
But now is the question why does Filemaker API rejects such an API Call?
SOLVED.
I was able to access one user in a FileMaker layout using Dio.
Dio dio = Dio();
dio.options.headers['content-Type'] = 'application/json';
dio.options.headers["authorization"] = "Bearer ${token}";
Response recordResponse;
recordResponse = await dio.post(
findUrl,
options: Options(followRedirects: false, validateStatus: (status)
{return status < 500;}),
data: { "query":
[{
"username": "=Jake",
"password": "=password"
}]
}
);
I can post input data using postman where as if I use the following code it's throwing an I/O error while reading the input message: nested exception is java.net.SocketTimeoutException(400)
$http({
method: "POST",
url: '/user',
headers: {'Content-Type': 'application/json', 'Accept': 'application/json'},
data: updateEmp
}).success(function (response) {
refresh();
}).error(function (response) {
alert(JSON.stringify(response));
//alert("There some problem while fetching.");
});