Passing csrf token when making PATCH/POST request to API #9935
Replies: 1 comment 1 reply
-
🤖 Hello, To add a CSRF token to the header for a PATCH/POST request to your API using the LangChain framework, you can modify the Here is an example of how you might modify the class _MockTextRequestsWrapper(TextRequestsWrapper):
@staticmethod
def post(url: str, data: Dict[str, Any], headers: Dict[str, Any] = None, **kwargs: Any) -> str:
if headers is None:
headers = {}
headers['X-CSRFToken'] = 'your_csrf_token'
# rest of the method...
@staticmethod
async def apost(url: str, data: Dict[str, Any], headers: Dict[str, Any] = None, **kwargs: Any) -> str:
if headers is None:
headers = {}
headers['X-CSRFToken'] = 'your_csrf_token'
# rest of the method...
@staticmethod
def patch(url: str, data: Dict[str, Any], headers: Dict[str, Any] = None, **kwargs: Any) -> str:
if headers is None:
headers = {}
headers['X-CSRFToken'] = 'your_csrf_token'
# rest of the method...
@staticmethod
async def apatch(url: str, data: Dict[str, Any], headers: Dict[str, Any] = None, **kwargs: Any) -> str:
if headers is None:
headers = {}
headers['X-CSRFToken'] = 'your_csrf_token'
# rest of the method... In the context shared, there is no mention or usage of a Also, the LangChain framework does have a built-in method for adding headers to requests. This is done in the Here is the relevant code: @classmethod
def from_llm_and_api_docs(
cls,
llm: BaseLanguageModel,
api_docs: str,
headers: Optional[dict] = None,
api_url_prompt: BasePromptTemplate = API_URL_PROMPT,
api_response_prompt: BasePromptTemplate = API_RESPONSE_PROMPT,
**kwargs: Any,
) -> APIChain:
"""Load chain from just an LLM and the api docs."""
get_request_chain = LLMChain(llm=llm, prompt=api_url_prompt)
requests_wrapper = TextRequestsWrapper(headers=headers)
get_answer_chain = LLMChain(llm=llm, prompt=api_response_prompt)
return cls(
api_request_chain=get_request_chain,
api_answer_chain=get_answer_chain,
requests_wrapper=requests_wrapper,
api_docs=api_docs,
**kwargs,
) In this code, I hope this helps! Let me know if you have any other questions. Sources
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Dear community,
I need to send a csrf token in the header when making a PATCH/POST request to our API.
The token I get via a GET request which works without issues.
But how to add the csrf token so that the llm can make a corresponding call to update entries?
Would using a TrackedRequestsWrapper be the recommended way to go?
Thanks a lot for the input!
Beta Was this translation helpful? Give feedback.
All reactions