Converting a cURL request to an Axios Post - http

I am working with the prometheus pushgateway library and am having trouble sending information to it. The examples use cURL which work well, but when I try to do the equivalent with AXIOS in my node program it throws an error.
I tried setting up an express server to send the same request to. I wanted to analyze the (REQ) and see how it was displayed. It was mangled by body parser and I am kind of lost.
cat <<EOF | curl --data-binary #- http://pushgateway.example.org:9091/metrics/job/some_job/instance/some_instance
# TYPE some_metric counter
some_metric{label="val1"} 42
# TYPE another_metric gauge
# HELP another_metric Just an example.
another_metric 2398.283
EOF
I have tried a few different things in axios, none of which have worked
- https://github.com/axios/axios#using-applicationx-www-form-urlencoded-format

request({
url: 'http://localhost:9091/metrics/job/interface_data',
method: 'POST',
body: 'datahere 10\n',
encoding: null
}, (error, response, body) => {
if (error) {
console.log(error)
} else {
console.log(response)
console.log('response, ', response.body)
}
})
Worked for me. The trick was encoding: null

Related

Handle 422 with $fetch

I'm upgrading a project to Nuxt3 and I'm using the new $fetch to hit my API, all it's OK but I can't manage the handle the 422 error from the API.
The API I've created would return something like this for the 422 code
{ message: "The email has already been taken.", errors: { email: ["The email has already been taken."]}}
Then in Nuxt I'm using this:
$fetch(`${runtimeConfig.public.BASE_API_BROWSER_URL}/XXX/`, {
method: 'POST',
body: {
...data
}
})
.then((resp) => {
message.value.push(resp.message)
isSubmitting.value = false
})
.catch((error) => {
errors.value.push(error.message)
isSubmitting.value = false
})
But what I have back is just FetchError: 422 Unprocessable Content instead of the error inside the API response, any idea on how to able to use error.message again?
Should be typically accessible via error.response.message.
For better visibility of the error object structure, use console.dir to explore its contents in the browser console: console.dir(error)
Unlike console.log(var1, var2, var3), console.dir only handles one attribute.
Important to remember this else you will be misled to thinking variables are empty.

Highlighted message in command prompt when running test on K6

I'm studying the execution of performance tests using K6.
Doing the Postman login test scenario, the result was positive:
Running the test with Postman
I exported the Postman collection and did the file conversion for K6 to recognize it.
Then, when running the test through K6, the following is returned:
Running the test with K6
This detail caught my attention:
✗ Successful POST request
↳ 0% — ✓ 0 / ✗ 1
Am I doing something wrong?
Thanks!
Edit: This is the print with the complete result after running the test.
Result
Edit 2: I'm putting the generated script that is being executed by K6, hiding only the address and login information:
// Auto-generated by the postman-to-k6 converter
import "./libs/shim/core.js";
import "./libs/shim/expect.js";
export let options = { maxRedirects: 4 };
const Request = Symbol.for("request");
postman[Symbol.for("initial")]({
options
});
export default function() {
postman[Request]({
name: "New Request",
id: "2140438e-8869-4836-ae80-7c5845d09572",
method: "POST",
address: "https://xxxx-xxxxxxx.xxxxxxx.xxx/public/auth/signin",
data: '{\r\n "username": "xxxxxxxxxxx",\r\n "password": "xxxxxx"\r\n}',
post(response) {
pm.test("Successful POST request", function() {
pm.expect(pm.response.code).to.be.oneOf([201, 202]);
});
}
});
}

Elixir HttpPoison not working to send multipart/form-data requests

I have a very simple request to make but it looks like HttpPoison isn't able to resolve this.
The request has attachments, so i'm using multipart/form-data content type.
When I send just the file, the request works fine, but I need to add some other props to my request and that's where the issue comes.
My request:
HTTPoison.post(
"path.com/api/anything",
{:multipart, [
{
:file,
"/path/file.xlsx",
{"form-data", [name: "file", filename: "file.xlsx"]}, []
},
{"taskName", "#{task.name}"},
{"taskLink", "#{task.link}"}
]},
)
I receive the file without problems but the taskName and taskLink never reach the server.
( I tried with postman and had no problems )
Some issues related to this:
https://elixirforum.com/t/httpoison-post-multipart-with-more-form-than-the-file/4222/4
https://github.com/edgurgel/httpoison/issues/237
We have a working example of the multipart list that we use to send zip files along with other attributes. Something equivalent to this might work for you.
[
{"id", to_string(order_id)},
{"file_size", to_string(file_size)},
{"attachment", file, {"form-data", [name: "file", filename: filename]},
[{"Content-Type", "application/zip"}]
}
]

FileSaver.js is saving corrupt images

This was working fine and suddenly stopped working. I'm not sure what exactly changed.
I need to download multiple images via URLs.
I'm using the following code:
https://plnkr.co/edit/nsxgwmNYUAVBRaXgDYys?p=preview
$http({
method:"GET",
url:"imageurl"
}).then(function(response){
saveAs(new Blob([response.data]), "image.jpg");
},function(err){
});
The files have different sizes, they are not 0 bytes.
For others coming here, check this solution.
What needed to be done was to add responseType: "blob" to the request:
$http({
method:"GET",
url:"imageurl",
responseType: "blob"
})
.then(...)
Here is the documentation for the responseType valid values, where it says the default is "" so the response is treated as text:
"": An empty responseType string is treated the same as "text", the default type (therefore, as a DOMString).
"blob:": The response is a Blob object containing the binary data.
Following code worked for me:
axios.get(`http://localhost:61078/api/values`, {
responseType: 'blob',
}).then(response => {
if (response) {
var FileSaver = require('file-saver');
FileSaver.saveAs(new Blob([response.data]), "image.png");
}
});

Fetch - sending POST data

My PHP script isn't receiving anything via POST from fetch. What am I doing wrong?
Javascript:
fetch("http://127.0.0.1/script.php", {
method: "post",
body: "inputs=" + JSON.stringify({param: 7})
}).then(response => response.json()).then(response => {
console.log(response);
});
script.php:
echo "\nprinting POST:\n";
print_r($_POST);
echo "\nprinting GET:\n";
print_r($_GET);
die();
Console:
printing POST: Array ( )
printing GET: Array ( )
You're not adding any url parameters (hence $_GET is empty) and you're not setting a Content-Type header that will make PHP automatically parse the request body. (hence $_POST is empty).
There was something wrong with the htaccess file for that particular test PHP file. I've used it for posting something to the main project folder/PHP files and it went ok.

Resources