Send multipart/mixed response using Koa - koa2

We need to send a multipart/mixed response (with a JSON part followed by one or more binary parts) using Koa. Note this is a response, not a request.
Does anyone know of any "out of the box" support for this, or do we need to roll our own?

Turns out this is nice and easy. We can use https://github.com/hendrikcech/multipart-stream to construct a multipart stream and then just set that as ctx.body.
For example, in the route handler:
const multipart = new Multipart();
multipart.addPart({
headers: {
"Content-Type": "application/json",
"Content-Disposition": `form-data; name="json"`
},
body: JSON.stringify(myJSONContent)
});
multipart.addPart({
headers: {
"Content-Type": "application/octet-stream",
"Content-Disposition": `form-data; name="binary-data"`
},
body: myBinaryBuffer
});
ctx.body = multipart;
ctx.response.type = "multipart/mixed";

Related

how to fix cors error in mongodb atlas data api

this is the code
HERE this is giving me the cors error. i also added the allow access origin header but the issue is not solved
let headersList = {
"Accept": "*/*",
"User-Agent": "Thunder Client (https://www.thunderclient.com)",
"api-key": "U5H4A6FcMbEuZ33LP0ACQHP0ydkXkGLLJnDfNzQzCXTpzxL8QdJ8tH7NocITeZvv",
"Content-Type": "application/json"
}
let bodyContent = JSON.stringify({
"collection":"users",
"database":"college",
"dataSource":"Cluster0",
"projection": {}
});
let reqOptions = {
url: " https://data.mongodb-api.com/app/data-tfdur/endpoint/data/beta/action/findOne",
method: "POST",
headers: headersList,
body: bodyContent,
}
axios.request(reqOptions).then(function (response) {
console.log(response.data);
})```
At this time, the MongoDB Data API does not support browser requests as CORS is not implemented.
There is a support page where you can vote to let them know the importance of this feature:
MongoDB: Please Support CORS from the Data API

Fetch 401 Authorization issue

I've started to build a project using WooComeerce and React.js, but I have one question.
When I try to get the data, for example: "products", received an issue 401 unauthorized..
I've tested my request into Postman/Insomnia and everything is working as expected. I think the problem is coming from the headers part, but for now I can't handle the issue..
Also my response headers are empty!
Here is my code:
const getProducts = async () => {
const response = await fetch(`${process.env.REACT_APP_API_URL}products?consumer_key=${process.env.REACT_APP_API_CONSUMER_KEY}&${process.env.REACT_APP_CONSUMER_SECRED_KEY}`,
{
method: "GET",
mode: "cors",
credentials: "include",
headers: {
"Authorization": `Basic ${process.env.REACT_APP_API_CONSUMER_KEY}`,
"Content-Type": "application/json",
},
}
);
return response;
};
How can i fix that issue?
Note: I'm not using WooCommerce REST API package.
Thank you in advance!
Usually the basic auth string is base64-encoded string username:password. However i see the consumer key being used in the req and the header. Can you please double check your authorization header?

Post request in redux-saga not working, nothing seems to happen

I would like to make post request in redux-saga. My code looks like:
var formdata = new FormData();
formdata.append("points", "a,b");
formdata.append("pid", "someid");
formdata.append("tions", "cses");
formdata.append("exp", "cx");
const ans = yield call(fetch,
'http://localhost:8000/api',
{
method: "POST",
headers: { 'Authorization': 'Token tokenid',
"Content-Type": "application/json",},
body : formdata
});
In Postman it is working fine (selected form-data in body), but in saga, the POST request is not happening at all.
Please suggest where I am doing wrong?
The problem is with your Exceptions on server-side, When you are throwing an error the headers are not set. So what you should do is with CORS Errors only with 400 bad request react fetch request.
Update :- The 400 Bad request has gone away after I added "Content-Type": "application/x-www-form-urlencoded" in the headers and changed the body type accordingly
Example :-
fetch("api/xxx", {
body: "email=test#example.com&password=pw",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
method: "post",
}

How to do an API call to Google Cloud Vision TEXT_DETECTION on Expo React Native

I am trying to do an API call to Google Cloud Vision. What should my request body look like?
I tried reading the documentation on how to format the request body but I am not getting any luck.
This is my request body
let image = this.fileToBase64().done();
let body = JSON.stringify({
requests: [
{
image: {
content: image
},
features: [
{ type: "TEXT_DETECTION", maxResults: "5"},
]
}
]
});
This is my call
let response = await fetch("https://vision.googleapis.com/v1/images:annotate?key=" + GOOGLE_API_KEY, {
method: "post",
body: body,
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
}
);
I am expecting a complete response in JSON with status code 200 but instead I am getting the response JSON with message "Request must specify image and features." and status code 400
Seems the problem was that the image content was empty as I did console.log(image). I just added the let image = await this.fileToBase64().done(); s the fileToBase64 is async

React native - post raw text data

I want to send this type data in raw text format using react native. please help-
submit Qsubmit_form=1&form_noresubmit_code=1525265560&question=OKKKK NEW APPP&submit=
You can use fetch API for both GET and POST requests. Example of POST request:
fetch('https://mywebsite.com/endpoint/', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
firstParam: 'yourValue',
secondParam: 'yourOtherValue',
}),
});
You can check out official docs for networking here

Resources