Comments (7)
I just encountered the same issue and would like to share my insights and the workaround.
tl;dr django.test.Client
requests default to the Content-Type=multipart/form-data
, this prevents the ipn
view from accessing request.body
after it accesses request.POST
. To work around it, pass a non-multipart content_type
:
client = django.test.Client()
client.post(reverse('paypal-ipn'), data=ipn_data, content_type='application/x-www-form-urlencoded')
I found the issue encode/django-rest-framework#2774 extremely useful when debugging.
It is mentioned by Preston Holmes in a comment that Django prevents reading a body of a request with the multipart/form-data
content type because "we don't want to repeat the expensive operation of parsing the multipart data".
While I'm fairly happy with the workaround, I'd hate to waste other users' time. So I would suggest we either:
- Catch the
RawPostDataException
in the view (add try/except ~ L45 and present a sane error which states we expect the POST to have Content-Type:application/x-www-form-urlencoded
, and it is something else). - Copy the request.POST/body before the first call and use the copy thereafter.
@spookylukey please let me know if you'd like to see a PR for either.
On a side note, it would be very helpful if Django docs mention the above behavior. I could not find mention of this anywhere besides this related middleware warning. Though the docstring added in this commit is very informative; I would go ahead and copy paste this directly to a warning in HttpRequest
docs. I've expressed my last paragraph in this Django ticket.
from django-paypal.
You'll need to paste the complete traceback. Thanks!
I suspect you have something (e.g. middleware or custom view logic) that is reading the request data, instead of letting the ipn view do that.
You can use the PayPal sandbox and something like ngrok to test locally.
from django-paypal.
I can't do anything without a traceback, so closing. Please reopen if you can provide more information.
from django-paypal.
Given that the PayPal docs explicitly show x-www-form-urlencoded (see https://developer.paypal.com/docs/classic/ipn/integration-guide/IPNIntro/#a-sample-ipn-message-and-response ) I think the best option here is to first check that the Content-Type matches that, and raise an exception otherwise. This avoids using internals like RawPostDataException
from django-paypal.
@spookylukey thanks for the quick response. I honestly couldn't find any mention (in quoted docs or any others) of the headers paypal sends. Right now however, the value of Content-Type
is clearly x-www-form-urlencoded
as can be seen by issuing a IPN Simulator request. And raising an exception if the behavior changes is a good way as any to handle this.
from django-paypal.
Is there a possibility this was changes recently? The server is getting many errors on the live server:
AssertionError: Invalid Content-Type - PayPal is only expected to use application/x-www-form-urlencoded. If using django's test Client, set
content_type explicitly
from django-paypal.
@jTiKey If PayPal changed the data to no longer match what their docs show - https://developer.paypal.com/docs/classic/ipn/integration-guide/IPNIntro/#a-sample-ipn-message-and-response - then you should take it up with them. I don't know if they have changed anything recently.
from django-paypal.
Related Issues (20)
- PayPalPaymentsForm action url HOT 1
- Upgrade or Downgrade subscription plan HOT 1
- PDT payments intermittently broken? HOT 4
- Listing the payments HOT 1
- Paypal notify url webook expecting x-www-form-urlencoded but received application/json from paypal HOT 1
- Testing with paypal sandbox and ngrok resulting in invalid postback HOT 1
- Received 3 IPNs for the same transaction, only one flagged as duplicate HOT 3
- Paying directly with a debit card HOT 1
- Working with react js HOT 1
- version 2.0 - update docs and tag HOT 3
- Encrypted form gives different interface on PayPal HOT 1
- Custom data got truncated HOT 4
- "Things don't appear to be working at the moment. Please try again later." HOT 3
- Add pay now button
- untested warning for is_subscription HOT 4
- How to use this sdk in django-rest-framework HOT 2
- Any pointers on how to integrate with Django Oscar?
- application/json vs application/x-www-form-urlencoded
- Increase the size of the logo HOT 2
- Add `Django 5.0` compatibility.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-paypal.