The issue:
On the server, I'm receiving my api_key parameter as api key (with space instead of underscore), how do I send it with underscore?
Code:
data = {
api_key: this.state.api_key
}
axios.post('/resource', data)
.then(response => { console.log(response) });
I'm using both React and axios (must use axios) on the frontend, and Rails on the backend.
Thanks
UPDATE
The rails action which first receives the parameters:
private
resource_params
ActiveModelSerializers::Deserialization.jsonapi_parse(params)
end
Axios serializes object params with JSON.stringify, which means the request is almost certainly being sent how you want (unless you are transforming it somehow). The issue is probably something on the Rails end. Looking at your resource_params method, I can see you are using ActiveModelSerializers's JSON API adapter. However, the request you are sending with Axios is not JSON API Compliant. You might try JSON.parse(params) instead or better yet, use Strong Params. Also you mentioned that you are using resource_params as a before action, but it doesn't look like you are assigning an instance variable there as one might expect. How are you handling that response? Are you calling that method directly in your action as well?
Anyway if that doesn't help I would be happy to continue our discussion in the chat.
After hours beating my head about this, I've found the culprit: Visual Studio Code
This is the output on VS Code:
And this is the output on linux terminal:
This is such a stupid issue but it never occurred to me that the bloody text editor would omit the underscores.
Trully #chris-g, this wasn't an issue with JSON or React, it was on the Rails side as #Xavier suspected, though the issue is unrelated to the lack of underscores themselves.
This is what I get for giving Microsoft some credit after so long.
Related
I haven't had this problem before and I've been doing it this way a long time.
(Angular Frontend)
const myFormData = new FormData();
myFormData.append("ok","true");
this.http.put(my_Express_backend_url,myFormData);
(Express Backend)
body-parser=require('body-parser');
...
console.log(req.body.ok)
undefined
everything is hitting right, the routes, the controller, it's getting to the proper function, but for some reason it does not work with the form data. I've used this method many times before and it is almost entirely identical yet I receive the undefined for the function i just wrote.
for now I am simply using javascript objects to pass info, eg:
const wantFormData={ok:'false'};
this.http.post(my_url,wantFormData);
...
console.log(req.body.ok);
...
false
But if somebody can help me get this back to normal I would greatly appreciate it! Thank you!
I think body-parser only works with JSON or text data. Take a look at http://expressjs.com/en/resources/middleware/body-parser.html and Parsing Post Form Data Node.js Express
For parsing FormData, you might need another package.
I have created an API in Ruby on Rails. Rather than sending the response body back with the API response, I am broadcasting it to an external service, which then takes care of the realtime distribution to all connected clients (i.e. WebSockets).
Today I have hit a snag where I realized that our 3rd party provider only allows data packets of up to 25kb. One of our responses has started giving problems as the data grew to be more than this limit and the 3rd party service has started blocking calls. As a side note, data packets will seldom grow to be greater than 25kb.
I did some research and was contemplating what the best idea would be. One idea I was thinking of, was to compress the response using ZLib and then to decompress it on the JS side. The article that led to this was this StackOverflow question.
I managed to get the deflation & Base64 encoding right, but could not decode on the JS side. I also tested the Base 64 string generated but services like this one, flags the base64 string as invalid.
My code looks like this:
In a Rails Controller
...
compressed_data = Zlib::Deflate.deflate(json_data.to_s)
encoded_data = Base64.encode64(compressed_data)
broadcast encoded_data
...
In JS that receives the broadcast:
import pako from 'pako';
import { decode } from 'js-base64';
...
const decoded = decode(payload.data);
const decompressed = pako.inflate(decoded);
...
When I execute the broadcast, I get the error: unknown compression method. I understand that this might have something to do with pako, but I have also tried other approaches with no success. Does anybody have any ideas or perhaps even a better way to approach this problem?
UPDATE:
The Base64 string generated in rails looks like this:
eJxlU2Fr2zAQ/SuHP25JkJW2abMPY6OllJWkNGFftmEU+RKLypKRTknD6H/f\nyQ5ru0EQutPdy3vvzr8LUxfz8nJ2NSrIkMViXkj4GjyGCNc+tBjgZxJCXsAq\nbbfePsF3NNa4XTEqaow6mI6Md9xWSgGPqm0RPkC+r8gjeW9yLDn+5vktRDPA\nYWrtJ4uwPBUIka9wr/qiCTze3N6t1o9f1nfLBTzcLK7vFref4cGiiggBdyYS\nU9scQRHkJEE5axjEO6AGoVZH2ODWB+xDlXRmOYGl0wgHhEbtM4xGs8cajj6F\nE2hQuXA0pGokZWyEg7GW4SCiIyDfQwa0uFccW4aI5PUTqF1+PzR+aNDekdKU\noXKT9m1nkQZCeyRiE6ELXmNkvWzniWRlvVYnT+/9gVUuQ4euVjyc16JIKlBV\nK+onJmQ5FuVYynU5nQvBv4kQ4qOQfHvTxCinFpesfc3TscswK2NZANdvTF0z\nuwqfCV0cqDj/JuSSriL1XIUeTXCcjjy3qgvYmtT2qRq3KlkmiZ2PhvqcTiGg\n00cGXKgF4+iADFFXigYdYlzKsTxbl2I+vZpPy4mUs786Ule/K+5Flyya32Uu\nvijL1+KIocrbPcv0gnK66cOzy1ER2fMsPMeDFSy5Mo7ZtGxBtVc2YSzmP0q2\ncSTzMc3HeT5yTvwaFU2/q9kG/oLOR0XLQzeaV7Hq0saa2FTkn9D9a7bl4VKq\n/xuC9W73/kHFNs+5H7HnFcCaZTlFKeTMATf5z/rvMO/VYEtuffkDW0lDVA==\n
Your data starts off with a zlib header, but the compressed data is corrupted for some reason.
I have created an app in Zapier Cli, that calls Fancyhands API. Everything works fine in Cli as I get proper response from API as shown below.
However, when I push this app to Zapier and try to use it. The API gives error, that one of the parameters is not formatted properly.
However, this shouldn't be the case, as I am sending same parameters from both CLI and the UI. It looks like the Zapier UI is adding some extra encoding to the parameters. Kindly suggest whats going on here?
The root issue is that fancyhands expects a UTC timestamp for the call_window_start param (like 2020-02-29T11:00:00Z). On the other hand, Zapier takes datetime fields and sends them to the developer in bundle.inputData as TZ-aware (2020-03-02T05:09:08-05:00).
The solution is therefore to remote the timezone data from the incoming timestamp. Maybe something like new Date(bundle.inputData.call_window_start).toISOString().
In your picture the datetime string is one time missing a Z at the end and the other time it has a Z.
Try adding/removing the Z.
If that does not work something may be wrong with the HTTP headers. Locally and on Zapier you might be sending the content as a json the other time as a string.
Make sure that also matches with the CLI.
I am currently configuring software, however, the fields I am configuring requires a very simple set of data (3 variable fileds) to be entered into a form. However, this operation needs to be done approximately 2,500 times to cover all cases. I can easily genrate a csv containing all the fields for all the entries of this tabel.
I am looking for high level information as to how this can best be accomplished using a script. Could somebody provide a crash course on how to incorperate a custom script to load and submit this information using an external file. Things such as: what fields should I look for using the inspect element or how to load and iterate through a file using JS or python, etc. Or provide any resources that would be helpful on how to accomplish this. Thanks in advance!
In this case I suggest using python rather than javascript, which can be executed conveniently in local.
At the beginning, you need a I/O. In case you are not familiar, take a look on this.
Next, you need a way to send http request. The library for this is urllib, which is inbuilt. Then you need a way to make everything in json, which is well-accepted type for data transferring through http.
You can find the sample of using urllib here.
Please remember that you are 'passing the data', use 'POST' method instead of 'GET' method.
Your data should be encoded in
from json import dumps
data= dumps({
//data dictionary
})
...
urllib.urlencode({'login' : 'MyLogin', 'password' : 'MyPassword', 'data': data})
You can ignore login and password just in case your backend doesn't require verification.
Welcome to ask me if you have any problem about this.
I am attempting to build a reader for Tiny Tiny RSS and am stuck almost at square one. I'm setting the app up to use ajax via prototype.js (1.6.1) and am attempting a simple login to the app to retrieve a key.
Here's what I have so far:
new Ajax.Request(Api.BASE_URL, {
method: "get",
parameters: {"op": "login", "user": "user", "password": "password"},
onSuccess: authSuccess,
onFailure: failure
})
Where Api.BASE_URL is defined as "http://tt-rss.example.com/api/"
When I try to log in I get the error response "NOT_LOGGED_IN", which appears to indicate that the api either does not recognize the op call that I am using, or the parameters are wrong, or something.
What is particularly odd is that this should be equivalent to the CURL command:
curl -d '{"op":"login","user":"user","password":"password"}' http://tt-rss.example.com/api/
Which works properly. I get the feeling that I am missing something incredibly simple, but am not sure what it is. Any help would be much appreciated.
In addition to the get vs post error, it turns out that the problem was that I was trying to pass a complex json object using the wrong ajax option.
I tested with a simpler function call (isloggedin), so my json object was defined as:
parameters: {"op": "isloggedin"},
When I used the "parameters" ajax option, the request was sending raw data in the form of:
op=isloggedin&_=
Which was getting rejected.
On the other hand, the raw data from cURL looked like:
{"op":"isloggedin"}
Which was working.
This post put me on the right track. I needed to use "postBody" instead of "parameters" to send the data properly. My final command wound up looking like this:
postBody: '{"op": "isloggedin"}',
And it worked. Also, apparently in this case type of quotes does matter. If I used single quotes on the outside, it worked. If I used double quotes on the outside if failed.
Your curl command sends data in a POST request, whereas the Ajax call uses the GET method. According to the tt-rss API documentation, older versions supported both GET and POST, but now data must be encoded using JSON in HTTP POST data.