0

I'm using opal-jquery to send an ajax request to my backend, but the json payload is malformed when I try to access it on the backend.

When I do puts @params.to_s on my back end, it gets displays as:

{"{\"username\":\"some_user\", \"password\":\"some_password\"}"=>nil}

So to get the login details I have to do:

@params.each do |k,v|
    login_details = JSON.parse(k)
end

My front end (opal) submit request looks like this:

    def handle_login_submit details
    url = "/client/api/auth"
    HTTP.post url, payload: details.to_json, dataType: 'json' do |response|
        case response.status_code / 100
        when 2
            puts response.status_code
            self.login_state = :return_success 
            puts response.json
        when 4
            self.login_state = :return_failed
            puts response.status_code
            puts response.json
        when 5
            self.f_message = {type: :error, message: "There was a server error" }
            self.login_state = :return_failed
        end

    end

end

I definitely know that details is correctly formatted so that's not an issue.

I guess in the end, it boils down to is whether it's

  1. opal-jquery sending the post request incorrectly

  2. sinatra improperly parsing the request.

I'm not sure which one it is, but I'm inclined to believe it's the former because it was working fine before I switched to using react.rb for the front end.

ylluminate
  • 12,102
  • 17
  • 78
  • 152
Thermatix
  • 2,757
  • 21
  • 51
  • I'd do a couple of things. Firstly, I'd watch `details` in the javascript console and see if it's what you think it is. Secondly, I'd use cURL to send a request with the values as you expect them to be and see how your backend handles that (or set up a spec to do it, hint hint;) One or the other will give you an idea of what's happening. – ian Aug 21 '15 at 10:29
  • "I definitely know that details is correctly formatted so that's not an issue." <- did you not read this? I wouldn't have said that if I hadn't already stuck it in the console to check, the value and the class both before and after being turned into json. I get that your trying to be helpful so thank you but please read the OP, otherwise you end up posting something that's already been done. – Thermatix Aug 21 '15 at 10:35
  • And yet you offered no evidence of that other than your personal assertion. Considering you're the one with the problem, and the one looking for help, you may consider being a bit less critical of others and tighten up the question. Good luck with getting further help with this ;) – ian Aug 21 '15 at 23:38
  • I'm not being critical, If you WANT me to treat people like they're stupid and can't think for themselves then ok, I'll speak slowly. But I don't because I expect people on this site to have a minimal level of intelligence, so when I say "I definitely know that details is correctly formatted so that's not an issue" I expect people to read this and go" Op must have already done some basic debugging through the console and seen that it was both a Json string and that the correct Key values were being sent"; or are you saying that people are in fact incapable and need be spoken to like idiots? – Thermatix Aug 24 '15 at 08:43
  • I try not to make assumptions when approaching a problem, it's not helpful, and I expect the person I'm helping to make allowances for my lack of knowledge of their situation - especially when it is incumbent upon them to provide as much information as they can, which you haven't done. More importantly, it seems you have an attitude problem more than a technical problem, I hope you get both of them resolved soon. – ian Aug 24 '15 at 10:33
  • I specifically wrote "**correctly formatted**", that means I've checked the formatting; to have checked the formatting means I've already put it to the console and seen that it's what I expect, if it wasn't I would have added it to the OP. The problem , whatever it is, comes after the formatting of the data; adding the correctly formatted data any way would have added nothing to the OP. What I have isn't an attitude problem, it's an irritation for people who don't read the whole question and then go on to ask for redundant information and then complain when it's pointed out. – Thermatix Aug 24 '15 at 11:33
  • I read that, you didn't *verify* it. It's up to you to produce that information, *no one else*, and if it's your word vs the Opal project's code, or the Sinatra project's code, my bet is the problem is with **you** - which would fit the general attitude of "it's not me" that you're giving off. Write those specs, you should get the basics right before whining to others. – ian Aug 24 '15 at 14:47
  • Whatever, I'm not going to let this question degenerate any further. If you want to be all pretentious about minor thing, do so. But for the record, what you're asking for isn't verification, it's asking me to treat anyone, you included, like a child that can't understand basic english. On a website like this I try to hold people to a better standard. That they're capable of reasoning through the OP and therefore of realising what is & isn't necessary to come to a conclusion about the problem; adding something just for the sake of adding it is just bloviating and therefore redundant. – Thermatix Aug 24 '15 at 15:35
  • You got me, I was wrong, you really are such a good coder that I was wrong to question your word, because your reputation is so high and your abilities so widely acknowledged as brilliant that I should toss aside those from 2 projects that actually have specs, because we're all humbled by your brilliance. Thank you for not treating us like children and like we all know basic English. If only we could write code and questions as good as you, you're raising the standard of this place no end! ;-) – ian Aug 24 '15 at 16:17

1 Answers1

0

After following the advice from this, specifically I'm doing:

before do
  request.body.rewind
  req = request.body.read
  puts req
  puts JSON.parse(req)
end

I was able to see that the request arrived at the server correctly and as expected. I don't know why but sinatra was mangling the response in such a away that @params came out as

{"{\"username\":\"some_user\", \"password\":\"some_password\"}"=>nil}

so, in the end I'm doing

before do
    request.body.rewind
    @request_payload = JSON.parse(request.body.read)
    request.body.rewind
end

To read out the request body before sinatra gets anything and just reading out the params I need from @request_payload.

Community
  • 1
  • 1
Thermatix
  • 2,757
  • 21
  • 51