axios.put save as a string - reactjs

if I try to use axios.put to save a string, it will give an error 400: Invalid data; couldn't parse JSON object, array, or value.
For example:
axios.put('firebase/url/example.json', "some string")
But if I use a integer, it will work perfectly:
axios.put('firebase/url/example.json', 200)
How can I save a string using put?

Firebase will try to parse "some string" using a JSON.parse-like function.
If you try it yourself, you will not be able to parse this:
JSON.parse("some string")
But if you encode the string properly, you will see it gets wrapped in more quotes:
console.log(JSON.stringify("some string"))
console.log("some string")
// will output:
// 1. "some string"
// 2. some string
The Firebase API docs highlight this with this example:
curl -X PUT -d '"Alan Turing"' \
'https://docs-examples.firebaseio.com/rest/saving-data/fireblog/users/alanisawesome/name.json'
So all you need to do is properly parse the data beforehand:
axios.put('firebase/url/example.json', JSON.stringify("some string"))
The reason 200 works, is that it's a single number which can be directly read by a JSON parser.

Related

Sending array of objects trough x-www-form-urlencoded

I want to send an array of objects trough a fetch call in react native in x-www-form-urlencoded format
global.modifiedOffline.forEach((elem)=>{
data["array[]"] = elem;
});
alert(data);
const formBody = Object.keys(data).map(key => encodeURIComponent(key) +
'=' + encodeURIComponent(data[key])).join('&');
This is what i've been trying to do but in the back end server i get a list of:
['[object Object]' ... ] which i cannot parse or use.
Currently, you are encoding objects. When you try to do that, encodeURIComponent tries to get a string representation of the object you supply it and [Object object] is the string representation in JS.
In order to be able to encode it, first you need to get it into a proper string format such as JSON as suggested by #Val. However, this part depends on the server side implementation you have. encodeURIComponent will only transform your string into URI component by replacing characters such as space with their escape counterparts (for example, %20).

Sumologic - split JSON array into multiple records

I am passing a JSON array object in the HTTP POST as
[{"level":"INFO","data": "Test 1"},{"level":"INFO","data": "Test 2"}]
This message is seen as 1 object/log message in SumoLogic. How can I tell SumoLogic to consider each JSON object as an independent object and show 2 log messages instead of one?
I believe this can't be done with the json operator. But, have a look at the docs for the "parse regex" operator. There's an option called "multi" which creates a new message for each match of the regex. In your case, something like this might do the trick:
parse regex "\{?<fieldname>.*?\}" multi
I didn't try this in the product itself, but here is Regex101 link to play with the regex.
I believe the actual answer to this is to not send your logs as an array. Instead include each json object in your body with a '\n' at the end for Sumo to consider these as individual log messages.
{"level":"INFO","data": "Test 1"}\n
{"level":"INFO","data": "Test 2"}\n

"no implicit conversion of Array into String" when trying to parse JSON data

I’m using Rails 4.2.3. I’m trying to parse JSON data, so I have
content = ["{\"sEcho\":3,\"timestamp\":1464705752942,\"iTotalRecords\":1242,\"iTotalDisplayRecords\":1242,\"aaData\":[{\"externalId\":\"4279\"}]}"]
my_object_times_array = JSON.parse(content)
Sadly, the second line gives the error
no implicit conversion of Array into String
The JSON is well-formed (at least as far as I can tell) so I’m not sure what is causing the error above and how to fix it. I would prefer not to change the JSON.
content is an array, but JSON.parse expects a JSON String.
Example of usage from the documentation:
require 'json'
my_hash = JSON.parse('{"hello": "goodbye"}')
puts my_hash["hello"] => "goodbye"
Check the documentation here
So you could do following:
content = "{\"sEcho\":3,\"timestamp\":1464705752942,\"iTotalRecords\":1242,\"iTotalDisplayRecords\":1242,\"aaData\":[{\"externalId\":\"4279\"}]}"
my_object_times_array = JSON.parse(content)
or
content = ["{\"sEcho\":3,\"timestamp\":1464705752942,\"iTotalRecords\":1242,\"iTotalDisplayRecords\":1242,\"aaData\":[{\"externalId\":\"4279\"}]}"]
my_object_times_array = JSON.parse(content[0])

string search in c from a http GET request

how to extract a specific string from an array of characters?
in my case buff contain what's shown in the image and i want to copy the file name 'hi.jpg' to another array of char, keep in mind that the file name will change depending on the http request sent by the browser.
You can use strstr(buff,"GET ") and strstr(buff," HTTP/1.1") to find the start end end pos, then use strncpy to copy whats in between.

convert the binary data to readable text

when using the ETCD dashboard, we find a binary string like
var _deps_js = "\x1f\x8b\x08\x00\x00\x09\x6e\x88\x00\xff\xcc\xbd\x7b\x77\xdb\x46\xb2\x2f\xfa\xff\xf9\x14\x22\xc6\x47\x01\xcc\x16\x25\xd9\x99\xb9\x7b\xc0\x40\xbc\x89\x1d\x27\xce\x24\x76\x26\x76\x26\x0f\x8a\xc9\xc2\x8b\x24\x24\x92\xa0\x49\xca\x92\x23\x72\x3e\xfb\xad\x5f\x55\x77\xa3\x01\x82\x72\xf6\xec\x7b\xd6\x3a\x99\xb1\x88\x47\xa3\x9f\xd5\xd5\xf5\xae\xce\xf8\x66\x91\x6e\x8a\x72\xe1\xc7\x2a\x09\xee\xbd\x32\xb9\xca\xd3\x8d\x17\x45\x9b\x0f\xcb\xbc\x1c\x1f\xcd\xcb\xec\x66\x96\x1f\x1f\x1f\x78\xd1\xcb\xef\x96\xe5\x6a\xb3\x1e\xd4\x6f\xa3\xb8\x97\x95\xe9\xcd\x3c\x5f\x6c\x06\x09\xd5\xdc\x39\x0b\xc2\xaa\xa1\xe0\xbe\x18\xfb\x9d\xaa\x48\xb0\x99\xae\xca\xdb\xa3\x45\x7e\x7b\xf4\xe5\x6a\x55\xae\x7c\xef\xea\x9f\x37\xf9\xea\xc3\xd1\x2a\x7f\x77\x53\xac\xf2\xf5\x51\x7c\x74\x5b\x2c\x32\x2a\x73\x5b\x6c\xa6\x74\x67\xbe\xf4\x82\xfe\x2a\xdf\xdc\xac\x16\x47\xd4\x4a\xb0\x0b\xf9\xaf\xef\xdd\x2c\xb2\x7c\x5c\x2c\xf2\xcc\xeb\x98\xee\xca\xf7\x03\xf9\x09\x37\xd3\x62\xad\xea\x23\x37\x77\x47\x29\x3a\xf8\x3e\x5e\x1d\x25\x34\x8a\x59\xbe\x98\x6c\xa6\x2a\x8d\xe2\xa4\x87\x9a\xe8\x9d\x6e\xd1\x33\x1f\xd0\x94\x44\xe9\x76\x4b\x05\x8a\xf5\x4f\x5c\x3d\x15\x1a\x74\xce\xc3\x73\x7a\x11\xf7\x16\x65\x96\xbf\xa5\x2f\x8f\x8f\x93\x41\xe7\x2c\xf4\xe2\xd5\x2a\xfe\xa0\xbf\x39\xa3\x9f\x64\xbb\xf5\x16\x37\xf3\x24\x5f\x55\x73\x9b\x50\xe1\x8b\x33\xfa\x73\x72\x7e\x54\x2c\x8e\xe2\x9d\xed\x5c\x86\xce\xaa\x94\x67\x90\x5b\x7c\x61\x06\x91\x04\x81\x9e\x0a\x7a\x3e\x59\xe5\x4b\x2a\xe
it's a long list.
Is there a tool to convert this into a readable text so that I can know the exact the content.
The two leading bytes "\x1f\x8b" indicate the data is gzip compressed.
You can use whatever gzip/gunzip library/utility you have to decode it.
Following is an example using Perl.
use IO::Uncompress::Gunzip;
my $in = "\x1f\x8b..."; # <- your string goes here
&IO::Uncompress::Gunzip::gunzip( \$in => \my $out );
print $out;
and it returns something like
!function(a,b){"object"==typeof module&&"object"==typeof module.exports?module.exports=a.document?b(a,!0):function(a){if(!a.document)throw new Error("jQuery requires a window with a document");return b(a)}:b(a)}("undefined"!=typeof window?window:this,function(a,b){function c(a){var b=a.length,c=ab.type(a);return"function"===c||ab.isWindow(a)?!1:1===a.nodeType&&b?!0:"array"===c||0===b||"number"==typeof b&&b>0&&b-1 in a}function d(a,b,c){if(ab.isFunction(b))return ab.grep
Since the string in your question is truncated, above output is truncated.

Resources