#Mist / Http body lost sometime

1 messages · Page 1 of 1 (latest)

marsh spindle
#

Hello,

sometimes (not always) the request body is lost on the server side (gleam mist/http) and its default value is an empty string.

I use python on the client side and send the same request several times to the gleam server to get the problem.

this may be a beginner's problem ?

blissful quarry
marsh spindle
#

Thanks a lot, it's much better now

OLD:

case req.body.body {
  Initial(data) -> {
    case bit_array.to_string(data) {
      Ok(data_str) -> {
        case data_str {
          "" -> content_not_valid
          _ -> {
            // process...
          }
        }
      }
      _ -> content_not_valid
    }
  }
}

NEW:

case mist.read_body(req, 4_000_000) {
    Ok(data) -> {
      case bit_array.to_string(data.body) {
        Ok(data_str) -> {
          case data_str {
            "" -> content_not_valid
            _ -> {
              // process...
            }
          }
        }
        _ -> content_not_valid
      }
    }
    _ -> content_not_valid
  }
}
blissful quarry
#

i should probably make it so you can't do that... so accessing the body like that is definitely not guaranteed to have any/all of the actual body of the request

marsh spindle
#

i edited my message, i pressed enter before finishing my message, it's my fault.

yes, you can access it directly in my old way, but there's no guarantee that you'll get the body content

#

So we can call read_body when gleam performs its process before the user's process, and so when the user accesses .body, it gets the actual body contents ?

full crow
#

btw the Wisp library will handle lots of tricky stuff around reading bodies etc, may be nicer for applications than using Mist directly.

marsh spindle
#

yes i saw that when i was looking for a module before starting, but my current project is not an application, it's just a tiny api where i need performance, so mist seems to be the best option for the project, with the carpenter module too, so that way i stay with rawhat's work, congrats by the way.

blissful quarry
#

haha thanks! the overhead of wisp is negligible if anything, so i would personally say unless you need websockets go for that

#

but yeah, we don't have the luxury of mutating properties, and also i wanted it to be flexible like... maybe you want to reject requests with large payloads or something. in which case, i didn't want to eagerly drop it into your handler

full crow
#

Wisp won’t add any runtime cost, nah