# How to handle validation errors from server after post request

**URL:** <https://discourse.elm-lang.org/t/how-to-handle-validation-errors-from-server-after-post-request/4236>\
**Category:** Learn\
**Created:** [August 30, 2019, 10:16am UTC](https://discourse.elm-lang.org/t/how-to-handle-validation-errors-from-server-after-post-request/4236 "2019-08-30T10:16:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nikolis\_Galerakis](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/nikolis_galerakis/32/2479_2.png) [@Nikolis\_Galerakis](https://discourse.elm-lang.org/u/Nikolis_Galerakis)\
**Post date:** [August 30, 2019, 10:16am UTC](https://discourse.elm-lang.org/t/how-to-handle-validation-errors-from-server-after-post-request/4236/1 "2019-08-30T10:16:31Z")

</div>

Hello all,

I am using post method to register a new user to the server and my command look like this

```
createPostCommand : User -> Cmd Msg                                                                      
createPostCommand user =                                                                                 
     Http.post                                                                                            
     { url = "http://localhost:4000/users"                                                                
     , body = Http.jsonBody (userEncoder user)                                                            
     , expect = Http.expectJson GotResult userDecoder                                                     
     }      

```

Me message type for the response is defined like  
GotResult (Result Http.Error String)

But my server upon HTTP 400 returns a list with all the validation errors like  
[“user\_name” : “can’t be blank”]

How can I set to the post method that upon 201 response I want it to use userDecoder  
and decode the User Returned but when 400 comes buck to use some userErrorDecoder  
and give me back the validation errors the server responded with ?

and consequently in my update method receive the appropriate response record ?

any help or direction towards an online resource, tutorial or even book would be appreciated.

Cheers

---

<div class="post-metadata">

**Author:** ![dmy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/dmy/32/702_2.png) [@dmy](https://discourse.elm-lang.org/u/dmy)\
**Post date:** [August 30, 2019, 10:39am UTC](https://discourse.elm-lang.org/t/how-to-handle-validation-errors-from-server-after-post-request/4236/2 "2019-08-30T10:39:26Z")

</div>

Have a look there to build your own expect function:

[https://package.elm-lang.org/packages/elm/http/latest/Http#expectStringResponse](https://package.elm-lang.org/packages/elm/http/latest/Http#expectStringResponse)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [September 9, 2019, 10:41am UTC](https://discourse.elm-lang.org/t/how-to-handle-validation-errors-from-server-after-post-request/4236/3 "2019-09-09T10:41:29Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
