DioError Http Status Error 405 when Using Dio.delete - flutter

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}

Related

Flutter http with authorisation: ClientException (Failed to parse header value)

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.

How to set `Content-Type` in headers in Axios?

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',
},
})

Flutter: Http Request has no body

I am trying to send a http request using the http plugin on version ^0.12.0+4 (the latest one).
http.Response response = await http.post(
http://localhost:8085,
body: jsonEncode({"test": "test"}),
headers: {
"accept": "application/json",
"content-type": "application/json"
},
);
I have a SpringBoot backend running which stated, that the request body is empty. Debug attempts with Postman worked fine.
I tried the Dio package as well with the same result.

Dio options.contentType vs header "Content-Type"

I was trying to make a call to a REST service using the Dio plugin, but kept getting HTTP 400 response code. I thought I was doing everything right by setting the content type and response type options to JSON:
Response response = await Dio().get(
'https://api.example.com/v1/products/$productId',
queryParameters: {},
options: Options(
contentType: ContentType.json,
responseType: ResponseType.json,
headers: {'Authorization': 'Bearer $MY_API_KEY'}
),
);
However, it turns out that I needed to add a Content-Type header as well:
headers: {'Authorization': 'Bearer $MY_API_KEY'}, 'Content-Type': 'application/json' };
So now I'm confused - what exactly does the contentType option do? I thought it was analogous to setting the Content-Type header manually?
I've tried this locally using dio: ^3.0.10 and it seems that ContentType.json is an invalid value for contentType.
Digging through the documentation for dio, Headers.jsonContentType should be used.

I am trying to pass value to the server in ionic using http but i am getting an error

see the bellow sample code which i am trying to send it to the server
var serializedData = $.param({
api: key,
user: $scope.user.username
});
$http({
method: 'POST',
url: URL+"login_master/login",
data: serializedData,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}})....