Send msgpack with ajax to flask (werkzeug) environment

msgpack data from the client side with js in a werkzeug environment I sent it as follows.

Client side

ninja.coffee


sendMsgPack:(event)=>
  event.preventDefault()
  arr = $('#hogehogeForm').serializeArray()
  data = msgpack.pack(arr)
  arrayBinary = new Uint8Array(data)
  xhr = new XMLHttpRequest()
  xhr.open('POST', '/api/url', true)
  xhr.onreadystatechange = ()->
  if xhr.readyState is 4 and xhr.status is 200
      console.log xhr.responseText
  else if xhr.readyState is 4 and xhr.status isnt 200
      console.log xhr, "hogehoge"
  xhr.setRequestHeader('Content-Type', 'application/x-msgpack; charset=x-user-defined')
  xhr.send(arrayBinary.buffer)

Server side

tasukete.py


import msgpack
from shimehari import Response, ApplicationController, request
import msgpack

class FooController(ApplicationController):
    def __init__(self, name):
        ApplicationController.__init__(self, name)

    def index(self, *args, **kwargs):
        return 'hidebu'

    def create(self, *args, **kwargs):
        #Let's really look at the header
        print msgpack.unpackb(request.data)
        return Response('aaa')
$ ({'name': 'name', 'value': 'aaaaa'},)

It seems that msgpack.js has a method called msgpack.upload. The method is PUT, or the Content-Type of the request header is ʻapplication / x-www-form-urlencoded`. I felt something immeasurable, so I used raw XMLHttpRequest for the time being. ArrayBuffer cannot be sent with jQuery.ajax, so it is useless.

When I set the Content-Type to x-www-form-urlencoded, I felt that it wasn't the case that the sent data was stored in Request.form instead of Request.data in werkzeug. I managed to do it.

At last, I decided to actually send it via WebSocket. Well, I feel that there are many situations where WebSocket cannot be used, but if you brush it up conveniently, it will be useful.

I think there are many places to go, so please let me know if you have any details.

Recommended Posts

Send msgpack with ajax to flask (werkzeug) environment
Send CSS compressed to Gzip with Flask
SNS Flask (Ajax) made with Flask
From environment construction to deployment for flask + Heroku with Docker
Build Flask environment with Dockerfile + docker-compose.yml
When you want to send an object with requests using flask
Change Python 64bit environment to 32bit environment with Anaconda
HTML email with image to send with python
Vue.js + Flask environment construction memorandum ~ with Anaconda3 ~
Send data to DRF API with Vue.js
How to upload with Heroku, Flask, Python, Git (4)
Send a message to LINE with Python (LINE Notify)
How to send a message to LINE with curl
Send an email to Spushi's address with python
Sample to send slack notification with python lambda
From Python environment construction to virtual environment construction with anaconda
Using Flask with Nginx + gunicorn configuration [Local environment]
Flow to complete Slack authentication with Flask (Python)
Virtual environment construction with Docker + Flask (Python) + Jupyter notebook
I want to transition with a button in flask
From Kafka to KSQL --Easy environment construction with docker
How to upload with Heroku, Flask, Python, Git (Part 3)
Output log to console with Flask + Nginx on Docker
Try to make RESTful API with MVC using Flask 1.0.2
How to upload with Heroku, Flask, Python, Git (Part 1)
termux × AWS Send smartphone location information to AWS with IoT
How to upload with Heroku, Flask, Python, Git (Part 2)
Ssh to virtual environment with remote development of vscode
Simulate temperature measurement with Raspberry Pi + Flask + SQLite + Ajax
Building an environment to use CaboCha with google colaboratory