Postman Is Your Freind Most Of The Time
Postman Rocks!
So in the past 3 or 4 weeks we have been building apps with teams of front end engineers, and my workflow goes like this. Come hell or high water GET registration and login working and deploy to heroku. The front end hackers can get plenty of things working in theory but very little meaningful progress can be made until signup/login is working. Once the app is deployed we are not so dependent on each-other to start some meaningful hacking. On the back end I can then begin to build out my controllers and test them using postman which does an awesome job of simulating requests from the front end (user interface). However…. in order to simulate arrays, or an array of hashes or whatever, there is a few formatting things you need to know about using postman.
The Postkraken
Sending data to our local server, or our app is a pretty straight forward process. Postman has a very easy to use gui to get most tasks done.But… what happens when you need to test a method like this
def creates
@words = params[:words]
@words.map do |word|
current_user.words.create!(word: word, category: params[:category])
end
@words= current_user.words
render "word_create.json.jbuilder"
end
This method is dealing with a bit of json that looks like this
{
word: => [dog, cat, bird, chicken, honeybadger],
category: => animals
}
The method works just fine, but I held off deploying the change because I could not figure out how to test it locally. Copying and pasting the horrendous errors postman was throwing out when I tried to pass a variable of an array with the key word:
actually broke google. Fortunatly one of my classmates Teri had run into a similar problem and showed me how to properly format the postman request, which looks like this.
Notice the brackets after word:
? This tells postman to insert the value into an array, and will perfectly simulate the json from above.
Victory!!!
So yeah I got to strut around for like ten minutes…feeling like a computer ninja! Front end can add as many words into a category as they want with a single request and my code will take care of the rest.
The Next Postkraken
Ok so my team can create words with a single request…great. Now they want to edit in a single request. In theory for me not so difficult to figure out…maybe. Long story short here is the javascript object (json) they would send me and the way to simulate such a request in postman.
{
words: => [
{id: => 1, new: => "kola"},
{id: => 2, new: => "kangaroo"},
{id: => 3, new: => "tiger"},
{id: => 4, new: => "dragon"},
{id: => 5, new: => "python"},
]}
Which will hit this method in my controller
def edit
params[words:].each do |new_word|
word = Word.find(new_word[:id])
word.update(word: new_word[:new])
end
end
And here is the way to properly format this
Again more brackets in the key side on postman. Also notice that it matters the order you list them in on postman. It has to go words[][id], words[][new], and so on.