title: How to send spacy doc object as payload in a REST api request and reconstruct the spacy doc object back inside the api?
body: <p>I have spacy object of type &lt;class 'spacy.tokens.doc.Doc'&gt;</p>
  <pre><code>import spacy
  import requests
  nlp = spacy.load(&quot;en_core_web_sm&quot;)
  spacy_doc = nlp(&quot;hello&quot;)
  </code></pre>
  <p>How can I send this object as payload in a api request? Currently when I try to post it using below code</p>
  <pre><code>requests.post(API_URL, json={'doc': spacy_doc}, timeout=3)
  </code></pre>
  <p>I am getting the below error</p>
  <pre><code>ERROR:Object of type 'Doc' is not JSON serializable
  </code></pre>
  <p>Doc.to_bytes() is mentioned in <a href="https://spacy.io/usage/saving-loading" rel="nofollow noreferrer">https://spacy.io/usage/saving-loading</a></p>
  <p>but when I use it</p>
  <pre><code>requests.post(API_URL, json={'doc': spacy_doc.to_bytes()}, timeout=3)
  </code></pre>
  <p>I get</p>
  <pre><code>ERROR:Object of type 'bytes' is not JSON serializable
  </code></pre>
  <p>What is the correct way to send spacy doc and reconstruct it back in the api(I am developing this target api service and its made using fastapi)?</p>