CodeRocks
CodeRocks

Reputation: 715

Django: Send a Post Request Through Form to Another Server

I have a form on a template on my domain1 and want to send a POST request to domain2. I am using Django as the framework. I only want domain2 to accept requests from domain1 and domain2 (itself). However, I run into csrf problems.

Upvotes: 0

Views: 2502

Answers (1)

Action Jackson
Action Jackson

Reputation: 1206

You need a RESTful API. That's a very large topic and would be dumb/impossible to do the work for you here with the info I've been given, so here's a summary.

I suggest Django Rest Framework for making api's.

What the above means, is that when you want to do this sort of stuff (POST requests from other domains), you need a token. This is usually done with a Json Web Token. Also known as JWT's.

The process goes like this:

  1. Get access token from other site to have permission to use the API from that site. Certain http headers, passwords, etc, are often included with this every single time data is exchanged.
  2. Once you get a response giving you permission, you can now perform the type of request you want, with the data you want.

The data format for all this is USUALLY done with JSON. So you will have to import json, then json.dumps(obj) to turn it into valid json, and json.loads(obj) to turn your response into a python dictionary, so you can use it in your template, or wherever you want.

If you want to see an example of this, I recently made a free GoFundMe clone on my guthub. In the donate() view at the bottom, it uses the requests library, and shows how a JWT is obtained and used. You can see it there.

Upvotes: 2

Related Questions