-2

all works fine in front end side , all with values and all well but wont arrive as class data and if i send a string in body they will arrive regular and work and method get all works i dont know what happening to this

this in C# asp.net


    [HttpPost]
            [Route("api/NewPost/Add")]
            public IHttpActionResult addPost([FromBody] Post newPost)
            {
                try
                {
                    string str = $"INSERT INTO [Post]([userId],[title], [description], [photos], [catalogType],[postDate],[typePost]) VALUES({newPost.userId},'{newPost.title}','{newPost.description}','{newPost.photos}','{newPost.catalogType}','{newPost.postDate}','{newPost.typePost}')";
                    SqlConnection con = connectionDb.connectToDb();
                    SqlCommand comm = new SqlCommand(str, con);
                    con.Open();
                    comm.ExecuteReader();
                    con.Close();
                    return Ok(true);
                }
                catch (Exception err)
                {
                    return Ok(err.Message);
                }
                return Ok(false);
            }

here what in js and all with value


     fetch('http://localhost:55866/api/NewPost/Add',{
          method:'POST',
          headers:{
            "Content-Type":"application/json"
          },
          body:JSON.stringify({postId:-1,userId,title,description,photos,catalogType:route.params.catalog,postDate:null,typePost})
    }).then(r=>r.json()).then(data=>{
    if(data === true){
      setAlertSucessfully(true)
    }
    }
    ).catch(err=>console.log('error :' + err.message))

here the class i tried to public all and wont work too arrive as null with problem

  •   $exception  {"Object reference not set to an instance of an object."}   System.NullReferenceException
    

    public class Post
        {
            // automatic post Id
            public int postId;
            public int userId;
            public string title;
            public string description;
            public string photos;
            public string catalogType; //dog - cat - other
            public DateTime postDate;
            public string typePost; //advice - ask - photo
     
    ```
            public Post(int postId,int userId, string title, string description, string photos, string catalogType, DateTime postDate, string typePost) 
            { 
                if (postId != -1)
                    this.postId = postId;
                else
                    this.postId = -1;
                Console.WriteLine(postId);
                this.userId = userId;
                this.title = title;
                this.description = description;
    
                if (photos == null)
                    this.photos = null;
                else
                    this.photos = photos;
    
                this.typePost = typePost;
                this.catalogType = catalogType;
                this.postDate = DateTime.Now;
            }
    
            public override string ToString()
            {
                return $"{postId} {userId} {title} {description} {photos} {typePost} {catalogType} {postDate}";
            }
        }

  • that is no code for mysql which would lead to an error,, this is also **vulnerable** to **sql injection** – nbk Jul 16 '21 at 15:09

1 Answers1

0

at first fix Post class by adding getter-setters

public class Post
        {
          
            public int postId  {get; set;}
            public int userId  {get; set;}
            public string title  {get; set;}
            
            ..... and so on
           }

and try this code

 fetch('http://localhost:55866/api/NewPost/Add',{
          method:'POST'
          body: { newPost: { postId:3,
             userId:4,
             title: "title", 
             description:"desciption",
             photos:null,
            catalogType:"catalog",
            postDate:new Date().toISOString(),
             typePost:null}}
      }).then(r=>r.json()).then(data=>{ .... your code

and action

 [Route("~/api/NewPost/Add")]
 public IActionResult addPost([FromBody] Post newPost)
.....
Serge
  • 40,935
  • 4
  • 18
  • 45
  • same problem all null – Mosaab Abo Rkia Jul 16 '21 at 15:03
  • @MosaabAboRkia - you have to fix the post class - see my answer – Serge Jul 16 '21 at 15:08
  • i just got results but if i sent for example userId as 9 or other within 0 they recieve there object.ToString returned "0 0 16/07/2021 18:13:53" string + this {PitCare.Controllers.UserController} PitCare.Controllers.UserController - newPost {0 0 16/07/2021 18:13:53} PitCare.Models.Post catalogType null string description null string photos null string + postDate {16/07/2021 18:13:53} System.DateTime postId 0 int title null string typePost null string userId 0 int str null string – Mosaab Abo Rkia Jul 16 '21 at 15:15
  • I am sorry , what do you mean? I don't understand. After my answer , do you have NullReference exception or not? – Serge Jul 16 '21 at 15:18
  • no its just comes as all 0 and null each int is 0 and each string is null but its not throwed as error throwed another error : System.Exception.Message.get returned "The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.\r\nThe statement has been terminated." string – Mosaab Abo Rkia Jul 16 '21 at 15:19
  • i tried all what you told me no affiance :( – Mosaab Abo Rkia Jul 16 '21 at 15:28
  • @MosaabAboRkia You had a wrong syntax, I fixed it , try it now. – Serge Jul 16 '21 at 15:32
  • again same problem its throw error – Mosaab Abo Rkia Jul 16 '21 at 15:49
  • @MosaabAboRkia It works properly at my computer. – Serge Jul 16 '21 at 16:19