Sending XML over HTTP with Rails - ruby-on-rails

I am dealing with a third-party api here and I need to send HTTP Post request represented in XML. How should I go about doing this in Rails? Which library/method if any will allow me to do this?

Try net/http package, in particular post method. There're examples too.
As to xml part, you can send any data you want as long as it's string.

A good starting point would be Net::HTTP library: http://stdlib.rubyonrails.org/libdoc/net/http/rdoc/index.html

Related

Nim - How to access raw POST request body in prologue framework

I recently picked up Nim and am in the process of re-implementing an existing web-application of mine to get some experience in the language.
This web-application used JWT for authentication, with the typical split into an access-token and a refesh-token.
The old way my application did refresh, was by receiving the refresh token via a POST request. The request body of that POST request would just be a raw JSON string and my application would grab the string off of that body and do its magic. The string would look like this:
{"refresh":"<JWT TOKEN STRING>"}
I've run into an issue when I wanted to access that raw JSON string in Prologue. There doesn't seem to be a way to do this.
When looking at the context's request, neither the PostParams nor the FormParams contain anything, they're empty. I can't find anything in the documentation about JSON-request bodies either and nothing in the source code looks like it is what I would want.
Is there no way for me to access the raw request body? Am I forced to change the way I send my refresh token?
After some more skillfull searching through the documentation I stumbled upon the answer I desired. There is a body() proc that allows you to access the raw HTTP body.

What is the advantage of using a GET http method to update values as opposed to POST http method?

I was reading up on how to create a telegram bot and I see that to set a web hook you use the GET http method. I assume that your link will be stored somewhere on telegram servers.
So now my question is:
Why not use the POST http method? What is the advantage of this design of using GET http method in this case rather than POST http method for something that pushes data?
Bot API supports GET and POST HTTP methods both. It's up to you what to use. (See this or this). I think it makes the API easy and fast to get started with.
For the sake of simplicity one might choose a simple GET request to set a webhook - Put together a Url with some parameters and call it in a web browser, done! Webhook is now set.
Still it is possible to do the same with a POST request (and you can argue it is the preferred way to do so). But it would need tools like Postman, CURL or some browser extensions.

Sending parameter with POST requests ,when content-type is multipart/form-data

I am new to JMeter. I am trying to create a test plan ,one of the requests is a POST request containing some parameter, the content type in the request header is Multipart/Form-data.
I am copying the headers/parameters from fiddler because the HTTP proxy recorder is not working.
Please see the image for the current settings I have.I am not able to get the required response using it.
As per HTTP Request Sampler Documentation
Use multipart/form-data for HTTP POST
Use a multipart/form-data or application/x-www-form-urlencoded post request
So all you need to do is:
Tick "Use multipart/form-data for POST" box
Remove all `Content-Disposition" lines
In regards to "proxy recorder not working", I have never experienced any problems with it so it might be misconfiguration or something like this. Some people find JMeter Chrome Extension easier to use.
Instead of copying the content-disposition etc, just send the parameters with name and you should be good. You are expected to send form data and it's value.
ideally it should look like, name should be just 'form' and it's value as 'buy-now'.
I would suggest you compare the requests that you are sending using developer tools and the request you are sending using JMeter, it will help you debug this quicker.
I hope it helps.

Ping/Post Form Handling with PHP?

I'm working with a company on lead delivery, and they sent me some info regarding a Ping Post form setup. I've built hundreds of HTML forms processed by PHP (ie. sending an email/etc), but never something that would Ping a url, then return a value. The value it returns is XML.
Here's the purpose of the process:
I send a lead (form data) using the form with a particular zip code
This company parses that info, decides if it wants to "buy" it
Returns XML saying "Approved" or "Denied"
If "approved", I then post the data, and if "denied", I can do whatever I want
What is a common PHP method for doing this? I can research the code and put something together, just need to know what structure or PHP methods would work?
Thanks in advance.
You should be looking into RESTful Web Services.
here's a few examples that might help you
http://markroland.com/blog/restful-php-api/
http://coreymaynard.com/blog/creating-a-restful-api-with-php/
I did not create these examples, just what I found on Google.
I used file_get_contents(url) to handle the posting. The url contains inputs from the HTML form added as a query string, and the response is in XML which gets handled with simplexml_load_file().
As far as I understand your question what you need is to make an HTTP POST request and parse the incoming XML data.
I would rather not use file_get_contents() on remote servers - there are some potential security issues and it was missing some features the last time I checked. I strongly recommend cURL for remote HTTP/HTTPS communication.
Depending on the API you are posting to you might be able to use the SOAPclient class, but from the look of the response you got all you need is XML parser or Simple XML.
Anyway if you just need to check if a certain keyword (like Approved or Denied) is present you can use a simple string matching like this
if(strpos($response,'<STATUS>APPROVED</STATUS')!==false){
//approved
}
...

POST via json in Ruby on Rails

I'm trying to create record in db via json. The problem is that i don't know how to compose http request in URL bar:
It should be something like:
http://localhost:3000/addnewpost.json?content=sometexthere
Or this is not correct?
It's not correct. Creating a record should be a POST command, not a GET. As such, the data content should go in the POST headers, not in the URL.
Are you sending this POST directly from Ruby? (if so, you'll want to look at Net::HTTP or some other ruby HTTP client). Show us some code and we'll help you improve it.

Resources