Skip to content
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

Change Default Content-Type for POST Requests to 'application/json' #1681

Merged
merged 15 commits into from
Oct 16, 2017

Conversation

tedrand
Copy link
Contributor

@tedrand tedrand commented Oct 11, 2017

Type of change

  • Bugfix
  • Feature
  • New bidder adapter
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other

Description of change

This change allows POST bid requests to be treated as JSON, by setting the Content-Type of the header.

While the docs for how to make a Prebid 1.0.0 adapter say that POST requests are treated as JSON, they are actually being treated as plain text currently.

Other information

I asked @mkendall07 about this bug and was told that it was worth putting in a pull request for.

@@ -249,7 +249,7 @@ export function newBidder(spec) {
typeof request.data === 'string' ? request.data : JSON.stringify(request.data),
{
method: 'POST',
contentType: 'text/plain',
contentType: 'application/json',
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the PR. I think we want to make this configurable however. Reason being that if it's application/json then CORS preflight request is required (additional round trip). text/plain doesn't have this requirement.

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, is the intention for 1.0.0 then for the endpoint to accept the POST request JSON payload in plain text format?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

how do you feel about an optional 'contentType' param on the spec object?

So it would be,

contentType: spec.contentType || 'text/plain'

Copy link
Contributor

Choose a reason for hiding this comment

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

The spec probably isn't a great place, because some adapters make several server requests. If a bidder wanted to make two requests with two different content types, they'd be stuck.

It would fit in pretty nicely on the ServerRequest object, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

works for me, thanks for helping out. We are just trying to avoid our current method of sending JSONP.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll get a new PR in shortly.

Copy link
Contributor

Choose a reason for hiding this comment

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

yep yep... the internet thanks you :)

tedrand added 2 commits October 11, 2017 15:27
…son', as this is the stated standard for Prebid 1.0.0."

This reverts commit 0338ce7.
@tedrand
Copy link
Contributor Author

tedrand commented Oct 11, 2017

Hmm.... can't tell why it is still failing the tests.

Copy link
Contributor

@dbemiller dbemiller left a comment

Choose a reason for hiding this comment

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

I know it's a small change... but I still think there should be a unit test for this, to prevent regressions.

@@ -249,7 +250,7 @@ export function newBidder(spec) {
typeof request.data === 'string' ? request.data : JSON.stringify(request.data),
{
method: 'POST',
contentType: 'text/plain',
contentType: request.contentType || 'text/plain',
withCredentials: true
}
Copy link
Contributor

@dbemiller dbemiller Oct 12, 2017

Choose a reason for hiding this comment

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

Ony minor thing... how do you feel about request.options.contentType, where options is an object?

Reason being: this could become Object.assign({}, theseDefaultPostOptions, requests.options), and then bidders will be able to edit any of the ajax options that they wanted.

Another benefit is that the JSDoc types would be reusable. The fourth argument to ajax would have the same typedef as the ServerRequest.options object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense to me. Should the Object.assign() route only be implemented/tested for the POST ajax call? Or would there be a reason to pass options on the GET too?

Copy link
Contributor

Choose a reason for hiding this comment

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

hmm... I guess I'd have a small preference towards doing it for both, just because parallelism makes the behavior easier for people to remember.

It's a pretty weak preference, though. At worst, the first adapter which passes options to GET will find out that it doesn't work, and then they'll just have to add it later.

@@ -224,6 +225,12 @@ export function newBidder(spec) {
requests.forEach(processRequest);

function processRequest(request) {
// check for undefined or null request.options
for (var member in request.options) {
if (!request.options[member]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

What if someone wanted to override withCredentials: false?

Is there any particular reason you're trying to delete null & undefined here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yeah, that was dumb my bad.

Copy link
Contributor

@dbemiller dbemiller Oct 13, 2017

Choose a reason for hiding this comment

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

I don't think so... I just ran it myself, and this test fails:

    it('should allow falsey options in the overrides', () => {
      const bidder = newBidder(spec);
      const url = 'test.url.com';
      const data = { arg: 2 };
      const options = { withCredentials: false }
      spec.isBidRequestValid.returns(true);
      spec.buildRequests.returns({
        method: 'POST',
        url: url,
        data: data,
        options: options
      });

      bidder.callBids(MOCK_BIDS_REQUEST);

      expect(ajaxStub.calledOnce).to.equal(true);
      expect(ajaxStub.firstCall.args[0]).to.equal(url);
      expect(ajaxStub.firstCall.args[2]).to.equal(JSON.stringify(data));
      expect(ajaxStub.firstCall.args[3]).to.deep.equal({
        method: 'POST',
        contentType: 'text/plain',
        withCredentials: false        // The test succeeds if this is true
      });
    });

Pretty sure you're deleting all false-y properties from the options object, so they don't override properly.

…ig for GET requests. Added test for this second change and removed tests for passing null or undefined member variables into the request.options object.
Copy link
Contributor

@dbemiller dbemiller left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -233,10 +234,10 @@ export function newBidder(spec) {
error: onFailure
},
undefined,
{
Object.assign({}, {
Copy link
Contributor

Choose a reason for hiding this comment

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

Tiny optimization: you don't need to Object.assign({}, defaults, overrides) here. Since the defaults are defined right here, Object.assign(defaults, overrides) should work just fine, and allocate one fewer object.

Definitely shouldn't hold up the merge though.

@dbemiller dbemiller added the LGTM label Oct 13, 2017
@mkendall07 mkendall07 merged commit a20c3f8 into prebid:master Oct 16, 2017
outoftime pushed a commit to Genius/Prebid.js that referenced this pull request Oct 18, 2017
* tag '0.31.0' of https://github.com/prebid/Prebid.js: (54 commits)
  Fix for prebid#1628 (allowing standard bidCpmAdjustment) (prebid#1645)
  Prebid 0.31.0 Release
  Support native click tracking (prebid#1691)
  Initial commit for video support for pbs (prebid#1706)
  Fixes: Immediate adapter response may end auction (prebid#1690)
  Rubicon feature/s2s test module (prebid#1678)
  Renaming of "huddledmasses" adapter into colossusssp (prebid#1701)
  Don't set non-object configurations (prebid#1704)
  Update JSDoc for `pbjs.enableAnalytics` (prebid#1565)
  Add ad units event (prebid#1702)
  AppnexusAst adapter: logging error message from endpoint (prebid#1697)
  AppnexusAst bidadapter markdown file (prebid#1696)
  Change Default Content-Type for POST Requests to 'application/json' (prebid#1681)
  Code improvement for trustx adapter (prebid#1673)
  PulsePoint Lite adapter - Enabling Sync pixel (prebid#1686)
  Update spotx video adapter to set the spotx_ad_key used in DFP (prebid#1614)
  Fix broken AOL mobile endpoint secure bid requests (prebid#1684)
  Fix adapter tests that hardcoded pbjs. (prebid#1666)
  no longer attaching gpt slots to adUnits, which breaks utils.cloneJson(adUnit) (prebid#1676)
  remove bidmanager from rubicon tests (prebid#1671)
  ...
vzhukovsky added a commit to aol/Prebid.js that referenced this pull request Oct 30, 2017
….31.0 to aolgithub-master

* commit 'e7341c948014a789084849495171d08d4b353d07': (21 commits)
  Added changelog entry.
  Fix for prebid#1628 (allowing standard bidCpmAdjustment) (prebid#1645)
  Prebid 0.31.0 Release
  Support native click tracking (prebid#1691)
  Initial commit for video support for pbs (prebid#1706)
  Fixes: Immediate adapter response may end auction (prebid#1690)
  Rubicon feature/s2s test module (prebid#1678)
  Renaming of "huddledmasses" adapter into colossusssp (prebid#1701)
  Don't set non-object configurations (prebid#1704)
  Update JSDoc for `pbjs.enableAnalytics` (prebid#1565)
  Add ad units event (prebid#1702)
  AppnexusAst adapter: logging error message from endpoint (prebid#1697)
  AppnexusAst bidadapter markdown file (prebid#1696)
  Change Default Content-Type for POST Requests to 'application/json' (prebid#1681)
  Code improvement for trustx adapter (prebid#1673)
  PulsePoint Lite adapter - Enabling Sync pixel (prebid#1686)
  Update spotx video adapter to set the spotx_ad_key used in DFP (prebid#1614)
  Fix broken AOL mobile endpoint secure bid requests (prebid#1684)
  Fix adapter tests that hardcoded pbjs. (prebid#1666)
  no longer attaching gpt slots to adUnits, which breaks utils.cloneJson(adUnit) (prebid#1676)
  ...
mattpr pushed a commit to mattpr/Prebid.js that referenced this pull request Oct 31, 2017
…rebid#1681)

* Update Sovrn adapter. Add test coverage. Enable deal IDs.

* HS-271: Avoid using private variables such as _bidsRequested and _bidsReceived in Sovrn adapter and Sovrn tests.

* lint

* Add bidfloor param to test.

* changed post content-type in bidder factory to 'application/json', as this is the stated standard for Prebid 1.0.0.

* Revert "changed post content-type in bidder factory to 'application/json', as this is the stated standard for Prebid 1.0.0."

This reverts commit 0338ce7.

* Changed method for altering contentType so that it is configurable via the ServerRequest object.

* Altered PR to conform to change reviews. added unit tests.

* Added comment to pass Trion adapter test.

* Removed false-y check for request.options. Added request.options config for GET requests. Added test for this second change and removed tests for passing null or undefined member variables into the request.options object.

* small optimization to request.options to remove extra object declaration.
@matthewlane matthewlane mentioned this pull request Nov 8, 2017
8 tasks
dluxemburg pushed a commit to Genius/Prebid.js that referenced this pull request Jul 17, 2018
…rebid#1681)

* Update Sovrn adapter. Add test coverage. Enable deal IDs.

* HS-271: Avoid using private variables such as _bidsRequested and _bidsReceived in Sovrn adapter and Sovrn tests.

* lint

* Add bidfloor param to test.

* changed post content-type in bidder factory to 'application/json', as this is the stated standard for Prebid 1.0.0.

* Revert "changed post content-type in bidder factory to 'application/json', as this is the stated standard for Prebid 1.0.0."

This reverts commit 0338ce7.

* Changed method for altering contentType so that it is configurable via the ServerRequest object.

* Altered PR to conform to change reviews. added unit tests.

* Added comment to pass Trion adapter test.

* Removed false-y check for request.options. Added request.options config for GET requests. Added test for this second change and removed tests for passing null or undefined member variables into the request.options object.

* small optimization to request.options to remove extra object declaration.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants