I want to upload media wordpress api with flutter - wordpress

I need to upload media wordpress api in flutter app, now I can upload media from wp api but the problem is the image after uploaded is broken and image size is very small, please help me
var resposeimg = await http.post(
'example.com/wp-json/wp/v2/media',
body: jsonEncode({
"file" : file.path.split("/").last,
}),
headers: {
'Accept-Encoding': 'gzip, deflate, br',
'Connection': 'keep-alive',
"content-type" : "image/${imgformat.replaceAll(new RegExp(r'jpg'), 'jpeg')}",
'Content-Disposition' : 'attachment; filename=${file.path.split("/").last}',
"Authorization": "Bearer ${widget.userToken}"
}
)

Related

Rails API not receiving fetch POST data from body

I have a route in Rails API '/api/pay'. My client side post request successfully hits the controller action, however nothing I send in the body: JSON.stringify('...') gets through to the back-end. Other post requests I have made work just fine with the same format.
export const payForItem = (payData) => {
return dispatch => {
dispatch(payForItemStart());
// ?userID=${data.userID}&adID=${data.adID}&price=${data.price}
const data = {userID: payData.userID, adID: payData.adID, price: payData.price}
fetch(`/api/pay`, {
method: 'POST',
header: {
'content-type': 'application/json'
},
body: JSON.stringify(data)
})
Here is what payData looks like.
Rails Api back-end params
Probably you've got typo in headers section. Should be plural headerS with s:
headers: {
"Content-Type": "application/json"
}

Python : POST a Multipart-Encoded File

Trying to upload a file using requests module, but encountered Internal Server Error Its the same using poster module too:
import requests
url = "abc.com/upload"
querystring = {"ft":"1","fn":"filename"}
payload = ""
files={'file': open(r'Users/.../test.zip', 'rb')}
headers_info = {
'content-type': "multipart/form-data; boundary=---12345",
'x-api-service-version': "1.0",
'connection': "Keep-Alive",
'authorization': "Basic XXXXXXX",
'x-file-format': "decrypted",
'cache-control': "no-cache",
}
response = requests.post(url, data = payload , headers=headers_info , params=querystring , files=files)
print response.status_code
print response.text
I tested the api with POSTMAN (chrome extension to test rest API) and it seems to work fine with postman i get a success response and the file is uploaded.
The postman code for python shows :
import requests
url = "abc.com/upload"
querystring = {"ft":"1","fn":"filename"}
payload = ""
headers = {
'content-type': "multipart/form-data; boundary=---12345",
'accept-encoding': "gzip, deflate",
'x-api-service-version': "1.0",
'connection': "Keep-Alive",
'authorization': "Basic XXXXXXX",
'x-file-format': "decrypted",
'cache-control': "no-cache",
'postman-token': "XXXXXXX"
}
response = requests.request("POST", url, data=payload, headers=headers, params=querystring)
print(response.text)
Any suggestions for the same ? Am I missing something obvious? Thanks for any pointers you can share!
You don't have to specify 'content-type': "multipart/form-data; boundary=---12345", as well as empty data. Try to send request without headers
response = requests.post(url, params=querystring , files=files)
If you fail you might try to add 'authorization': "Basic XXXXXXX", 'postman-token': "XXXXXXX" headers

dart BrowserClient - how to read response headers?

I don't manage to read response headers using browser_client.dart :
import 'package:http/browser_client.dart';
var response =
await client.post(url, headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}, body: body);
print('Response headers: ${response.headers}');
Thanks for your help.
The server needs to allow the browser to expose the headers by listing the headers in the Access-control-expose-headers response header, otherwise you can see them in the browser devtools but when you try to read them in code, the browser will suppress them.
See also
https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
Why is Access-Control-Expose-Headers needed?

Meteor HTTP response content un readable

This Meteor server code uses atmosphere HTTP package. I receive human un readable characters from response.content even though characters are readable fine in the browser.
Why and how to fix that? Thanks
const response = HTTP.call(method, url, {
timeout: 30000,
headers: header,
params: Params,
followRedirects: true
}
);
console.log(response.content);
response header has:
'content-type': 'text/html'
'content-encoding': 'gzip'
request header has:
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en-US,en;q=0.5",
"Content-Type": "application/x-www-form-urlencoded"

request headers not submitted during post request using the WiFlyHQ lib

using the WiFlyHQ library i try to send an POST request, it seems like the request header get cropped, if i inspect the request on the server i can see the following:
headers: { host: 'localhost:3000', 'content-type': 'application' },
with this setup;
void SendJasonPacket()
{
wifly.open(Server, ServerPort);
wifly.println("POST / HTTP/1.1");
wifly.println("Host: localhost:3000");
wifly.println("Content-type: application/json");
wifly.println("Accept: application/json");
wifly.println("Content-Length: 93");
wifly.println("User-Agent: easyNAM/0.0.1");
wifly.println("{'checkin':{'device_token': '122','card_token': '12312', 'timestamp': '2012-10-29T14:31:03'}}");
wifly.close();
}
i tried a couple of different headers, that's what i got:
headers: { 'user-agent': 'easyNAM/0.0.1', accept: 'application/j' },
headers: { accept: 'application/json', 'user-agent': 'easyNAM/0' },
headers: { host: 'localhost:3000', 'content-type': 'application' },
it seems, that it get cropped after a specific character count, not sure what i did wrong here....
I believe memory is the issue, i ran into the same issue. I am using VS 2012 to build my app and when it reaches 60% it tends to act sporadically.

Resources