Skip to content

Added jsonp support #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 6, 2014
Merged

Added jsonp support #35

merged 7 commits into from
Jun 6, 2014

Conversation

arnold-almeida
Copy link
Contributor

Heya. Here is a semi nice way of providing jsonp support without having to modify too much.

@jasonlewis
Copy link
Contributor

I'm not worried about modifying it a bit as this is still in major version zero. I'll just bump the minor release for this.

Instead of the RequestAwareInterface the setRequest method should be on an abstract ResponseFormat class. That way the request is set on every formatter regardless. So basically, remove the ResponseFormatInterface and replace it with an abstract ResponseFormat class. Make sense?

@arnold-almeida
Copy link
Contributor Author

Yeah cool. I wasn't sure if you were ok with making the response available to every formatter.

Ill update sometime this week.

@jasonlewis jasonlewis mentioned this pull request May 12, 2014
mohitmamoria and others added 3 commits May 14, 2014 20:30
@arnold-almeida
Copy link
Contributor Author

Hiya, Have a look and let me know. If thats ok.

@terion-name
Copy link

yeap, jsonp is needed a lot

@jasonlewis jasonlewis mentioned this pull request May 22, 2014
$response->setStatusCode(200);
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain this? Why is the response being forced to return as "200 OK"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah its annoying but i was working with another developer and they were
implementing something in phone-gap.

Apparently when they made jsonp requests and the response status code was a
non standard one eg. 422, the response body would be dropped.

Using a 200 ensured that the body was delivered. I havent had time to
verify this personally, so perhaps someone else can confirm / shed some
light on this edge case.

On Thu, May 22, 2014 at 9:38 PM, Jason Lewis notifications@github.comwrote:

In src/Routing/Router.php:

        $response = Response::makeFromExisting($response)->morph($this->requestedFormat);
    }
  •   // Force the response to always return "200 OK"
    
  •   // Chances are if the client is requesting jsonp their browser
    
  •   // wont implement 422 etc and will drop the body
    
  •   if ($this->requestedFormat == 'jsonp') {
    
  •       if (!in_array($response->getStatusCode(), [404, 200])) {
    
  •           $response->setStatusCode(200);
    
  •       }
    
  •   }
    

Can you please explain this? Why is the response being forced to return as
"200 OK"?


Reply to this email directly or view it on GitHubhttps://github.com//pull/35/files#r12945612
.

Arnold Almeida
+61 432 149 788
arnold@floatingpoints.com.au

Floating Points // Digital Direction
floatingpoints.com.au

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'm not too sure on this part. Seems a bit hacky to me.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

422 is a client error. 200 means everything is fine.

This is terrible. I have a whole slide about the problems this causes in my talk.

Please don't do this.

422 is often used for validation, but it is reserved for WebDAV and not officially in the HTTP spec. Just tell whoever is using PhoneGap and 422's to switch to 400 and you'll be fine.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 422's I believe are hardcoded into the dingo exceptions for store, update, and delete. But I could be mistaken. 

---Hunter Skrasekhunterskrasek@me.com

On May 23, 2014 at 5:16:30 PM CDT, Phil Sturgeon notifications@github.com wrote:In src/Routing/Router.php: > $response = Response::makeFromExisting($response)->morph($this->requestedFormat); > } > > + // Force the response to always return "200 OK" > + // Chances are if the client is requesting jsonp their browser > + // wont implement 422 etc and will drop the body > + if ($this->requestedFormat == 'jsonp') { > + if (!in_array($response->getStatusCode(), [404, 200])) { > + $response->setStatusCode(200); > + } > + } > + 422 is a client error. 200 means everything is fine. This is terrible. I have a whole slide about the problems this causes in my talk. Please don't do this. 422 is often used for validation, but it is reserved for WebDAV and not officially in the HTTP spec. Just tell whoever is using PhoneGap and 422's to switch to 400 and you'll be fine. —Reply to this email directly or view it on GitHub.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should be changed too then. :)

-- 
Phil Sturgeon

On May 24, 2014 at 12:02:18 AM, Hunter Skrasek (notifications@github.com) wrote:

In src/Routing/Router.php:

                   $response = Response::makeFromExisting($response)->morph($this->requestedFormat);
       }
  •      // Force the response to always return "200 OK"
    
  •      // Chances are if the client is requesting jsonp their browser
    
  •      // wont implement 422 etc and will drop the body
    
  •      if ($this->requestedFormat == 'jsonp') {
    
  •              if (!in_array($response->getStatusCode(), [404, 200])) {
    
  •                      $response->setStatusCode(200);
    
  •              }
    
  •      }
    
    The 422's I believe are hardcoded into the dingo exceptions for store, update, and delete. But I could be mistaken. 


    Reply to this email directly or view it on GitHub.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clearing that up Phil.

Yeah the 422's are coming from the api internals. Will amend and fix in a
bit. If 422 is for WebDAV is make sense for phonegap not to support it.

On Sat, May 24, 2014 at 9:03 AM, Phil Sturgeon notifications@github.comwrote:

In src/Routing/Router.php:

        $response = Response::makeFromExisting($response)->morph($this->requestedFormat);
    }
  •   // Force the response to always return "200 OK"
    
  •   // Chances are if the client is requesting jsonp their browser
    
  •   // wont implement 422 etc and will drop the body
    
  •   if ($this->requestedFormat == 'jsonp') {
    
  •       if (!in_array($response->getStatusCode(), [404, 200])) {
    
  •           $response->setStatusCode(200);
    
  •       }
    
  •   }
    

That should be changed too then. :)
-- Phil Sturgeon On May 24, 2014 at 12:02:18 AM, Hunter Skrasek (
notifications@github.com) wrote: In src/Routing/Router.php:
$response =
Response::makeFromExisting($response)->morph($this->requestedFormat); } +
// Force the response to always return "200 OK" + // Chances are if the
client is requesting jsonp their browser + // wont implement 422 etc and
will drop the body + if ($this->requestedFormat == 'jsonp') { + if
(!in_array($response->getStatusCode(), [404, 200])) { +
$response->setStatusCode(200); + } + } +
The 422's I believe are hardcoded into the dingo exceptions for store,
update, and delete. But I could be mistaken. … — Reply to this email
directly or view it on GitHub.


Reply to this email directly or view it on GitHubhttps://github.com//pull/35/files#r13022269
.

Arnold Almeida
+61 432 149 788
arnold@floatingpoints.com.au

Floating Points // Digital Direction
floatingpoints.com.au

- Use 400 instead of 422 for increased compatibility for clients.

See comments in dingo#35 for rational.
@arnold-almeida
Copy link
Contributor Author

Do i need to do anything else ?

@jasonlewis
Copy link
Contributor

I'll pull this down locally and merge it. There's a couple things that are changing along the way.

@junxy
Copy link

junxy commented Jun 5, 2014

👍

@jasonlewis jasonlewis merged commit 51538f7 into dingo:master Jun 6, 2014
@ingro
Copy link
Contributor

ingro commented Jul 8, 2014

Can someone provide an example of using this? Thank you!

arnold-almeida added a commit to arnold-almeida/api that referenced this pull request Oct 6, 2014
- Use 400 instead of 422 for increased compatibility for across API clients.

See comments in dingo#35 for rational.
@j0an
Copy link

j0an commented Jan 12, 2015

+1 @ingro

@camilstaps
Copy link

@ingro @j0an

All you need to do is:

  1. Override Dingo's config.php (i.e. create a /app/config/packages/dingo/api/config.php to use the JsonP formatter:

    return [
       'default_format' => 'json',
        'formats' => [
            'json' => 'Dingo\Api\Http\ResponseFormat\JsonpResponseFormat',
        ]
    ];
    
  2. Add ?callback=console.log to the URL. this will call console.log() when the request is finished.

Note that when ?callback is not present, the formatter will default to normal json. Hope this helps others in the future, it took me an evening to figure this out. Perhaps this could be made more clear in the docs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants